0% found this document useful (0 votes)
9 views7 pages

C++ Access Specifiers in cPerson Class

Uploaded by

desano5736
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

C++ Access Specifiers in cPerson Class

Uploaded by

desano5736
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C ++
Access specifiers
In C++, there are three main access specifiers that control how members
(variables and functions) of a class can be accessed:

1. public: Members declared as public are accessible from anywhere in your


program. They can be directly accessed by objects of the class, other
functions, and even outside the file where the class is defined.

2. private: Members declared as private are encapsulated within the class. They
can only be accessed directly by member functions of the same class. This
enforces data hiding and promotes better code organization.

3. protected: Members declared as protected are similar to private members but


with an added benefit. They can still only be accessed directly by member
functions of the same class, but also by member functions of derived classes
(inheritance concept). This allows controlled access within a class hierarchy.

Here's a table summarizing the access specifiers:

Access Specifier Meaning

public Accessible from anywhere in the program

Accessible only by member functions of the


private
same class

Accessible by member functions of the


protected
same class and derived classes

Use of multiple Files-cPerson with C++ Standards


cPerson.h

C ++ 1
#ifndef CPERSON_H
#define CPERSON_H

#include <iostream>
#include <string>

using namespace std;

class cPerson
{
public:

cPerson();

~cPerson();

void setName(string argName);


void setAge(int argAge);
string getName();
int getAge();

void who();
protected:

private:
string name;
int age;

};

#endif

[Link]

C ++ 2
#include "cPerson.h"

cPerson::cPerson()
{
name=""; age=0;
}

void cPerson::setName(string argName)


{
name=argName;
}
string cPerson::getName()
{
return name;
}

void cPerson::setAge(int argAge)


{
age=argAge;
}
int cPerson::getAge()
{
return age;
}
void cPerson::who()
{
cout<<endl;
cout<<"Name:\t"<<name<<endl;
cout<<"Age: \t"<<age<<endl;
}

cPerson::~cPerson()
{

C ++ 3
//dtor
}

[Link]

#include <iostream>
#include "cPerson.h"

using namespace std;

int main()
{
cPerson john;
string s;
int x;

cout<<"Name= "; getline(cin,s);


[Link](s);

cout<<"Age =";cin>>x;
[Link](x);

[Link]();

return 0;

Explanation:

cPerson.h (Header File):

Header Guards:

#ifndef CPERSON_H and #define CPERSON_H prevent multiple inclusions of this


header file, ensuring it's included only once.

#endif marks the end of the header guard block.

C ++ 4
Includes:

<iostream> : Provides input/output functionalities like cout and cin .

<string> : Provides string manipulation capabilities like string and getline .

Namespace:

using namespace std; : Avoids repetitive use of std:: before standard library
elements like cout and string (cautionary note: This practice can lead to
naming conflicts in larger projects, so consider using it selectively).

Class Definition:

class cPerson : Declares a class named cPerson to represent a person.

[Link] (Implementation File):

Include:

: Includes the header file cPerson.h containing the class


#include "cPerson.h"

declaration, member function prototypes, and other necessary definitions.

Member Function Implementations:

Constructor:

cPerson::cPerson() : Initializes a cPerson object. Assigns an empty string to


name and 0 to age .

Setters:

void cPerson::setName(string argName) : Sets the name member variable to the


provided argName string.

void cPerson::setAge(int argAge) : Sets the age member variable to the


provided argAge integer (note: No age validation is performed in this
example).

Getters:

string cPerson::getName() : Returns the current value of the name member


variable.

int cPerson::getAge() : Returns the current value of the age member variable.

who() Function:

C ++ 5
void cPerson::who() : Prints the person's name and age using cout .

Destructor:

(commented out): Destructors are typically used to


cPerson::~cPerson()

clean up resources before an object goes out of scope. However, in this


basic example, there are no specific resources to manage.

[Link] (Main Program):

Includes:

<iostream> : Included for input/output operations.

"cPerson.h" : Includes the header file to access the cPerson class definition.

Namespace:

using namespace std; (cautionary note: Same as in cPerson.h ).

Main Function:

int main() : The entry point of the program.

Object Creation:

cPerson john; : Creates an object of the cPerson class named john .

Input:

Prompts the user to enter the person's name and age using cout and
cin .

Reads the name using getline(cin, s) to handle potential spaces.

Reads the age using cin >> x .

Setting Values:

Calls [Link](s) to set the person's name in the object john .

Calls [Link](x) to set the person's age in the object john .

Printing Information:

Calls [Link]() to print the person's name and age stored in the object
john .

Returns 0: Indicates successful program execution.

C ++ 6
Overall Functionality:

This code defines a cPerson class to represent a person with a name and age. It
allows creating cPerson objects, setting their names and ages, and displaying the
information using the who() function.

C ++ 7

You might also like