Class -> It is a user defined data type which holds its own data members and member functions.
Syntax:
class class-name
{
variable declaration;
function declaration;
}
eg: class Person
{
private:char name[20[; //variable declaration
int roll;
public: void getdetails();
void displaydetails(); //function declaration
};
Access Specifiers
There are 3 access specifiers.
1.Private access specifiers-> In this specifier,class members can be access by members functions of that class only.
2. Protected access specifiers-> In this specifier, class members are not able to access outside the class but can be accessed by the derived class of that class.
3. Public access specifiers-> In this specifier,class members can be accessed from anywhere in program.
Member function can be defined in two ways.
1. Inside the class body
eg, class Person
{
private:
char name[20];
int age;
public: (For more example program:https://veveek2.blogspot.com/2021/03/inside- class.html) Click to link
void input()
{
cout<<"enter name:";
cin>>name;
cout<<"enter age:";
cin>>age;
}
void display()
{
cout<<"Name:"<<name<<"Age"<<age;
}
};
2. Outside the class body
eg. class Person
{
private:char name[20];
int age;
public:void input();
void display();
};
void Person::input() //scope resolution :: is used to define the member function outside the class body
{
cout<<"enter name:"; (For more example program click to link: https://veveek2.blogspot.com/2021/03/outside-class.html)
cin>>name;
cout<<"enter age:";
cin>>age;
}
void Person::display()
{
cout<<"Name:"<<name<<"Age"<<age;
}
Comments
Post a Comment