class in C++
as already mentioned in the OOPs page. class is a collection of data members and function members which are logically bind together in a set. These members are declared in some access-specifier or mode.
“members of a class are made accessible with their class-object, using access specifier class can be implemented.”
Thus by defining access mode in a class, the syntax of a class would be as follows –
class class-name
{ private: data member; function member; public: data member; function; };
Here, data member representing the data type (int, char, float
) while member function is representing the function type members. It has a class-name an identifier i.e. any appropriate name (except reserved keyword).
before starting programming with class, we will discuss these access specifier or visibility-mode.
Type of specifier in C++
- Private specifier
- Public specifier
- Protected specifier
Private specifier
When a member of a class is declared in private mode, it can only be accessed by same public members and friend function(non-member function) only.
✍: private members cannot be accessed from the outside of a class and can’t be inherited also.
However, all members of a class are by default private so if there is no mode defined in the class, then all members of a class will be private automatically. i.e. private
keyword is optional here. such as
class class-name
{
data-member;
function-member;
}
such as,
Public specifier
In this mode, the declared data members and function members can be accessed outside the class from its object or class-variable.
a class private members will be accessed by its public members and friend function while public members will be accessed by the class-object. such as
✍: public members can be accessed from the outside of a class and can also be inherited.
because all members of a class are default private. Therefore, we use public
keywords to make members public. such as,
class class-name
{
data-memeber;
function-member;
public:
data-member;
function-member;
}
apart from two access modes, a third access mode is also found in the class which is used to inherit the property and behaviour of a class in another class. This is a mode called protected mode or protected specifier.
Protected specifier
This specifier of a declaration of members is the same as private, that is, the members declaring in both modes can not be accessed from the class object.
the main difference between Private and Protected is that protected members can be accessed by inheriting another class (a feature of inheritance) while private-members are accessible only for its class members.
we use the protected
keyword to protect members of a class.
We will read about the protected mode in inheritance. here we will discuss private and public members.
access specifier in inheritance
Example of a default class in C++
In the below Program roll_no and name, both are Private member by default while get_record() and put_record(), declared as a public member. now, these private members are accessed by the same class called “student”. such as
Here is the Program,
#include<iostream>
#include<stdio.h>
using namespace std;
class student
{
// private by default
int roll_no;
char name[20];
public: // public member
void get_record(void);
void put_record(void);
}; class closed
//input record from the user
void student::get_record()
{
cout<<"Enter roll no: ";
cin>>roll_no;
cout<<"Enter Name : ";
gets(name);
}
//display record
void student::put_record()
{
cout<<"\nRoll no: "<<roll_no;
cout<<"\nName : "<<name;
}
// main program start
int main()
{
return 0;
}
above Program will not get any OUTPUT Because
“a class can not be implemented without declaring it’s class-variable or object.”
declaring a class-object in C++
just as the member of the structure is accessed from the structure-variable, the class member is accessed by class-variable or object. remember private member will not be accessed by an object.
Only class public members will be accessed by an object as shown in public specifier’s diagram.
An object is an identifier of any appropriate name (except reserved keyword).
syntax
class-name object;
Where object is an identifier (except reserved keyword) a class can have more than one object with different name.
A class can have more than one object (different) –
class-name object1, object2,.....n;
Here is another way to declaring an object in the Program,
class class-name
{
data-member;
public:
data-member;
member-function;
} object1, object2,.....n;
Accessing class member using their object
as we know only public members of the class can be accessed. Hence-
after object declaration, using first object x,
x.get_record();
x.put_record();
then using second object y,
y.get_record()
y.put_record();
Here two object x and y indicates two students. the more objects there are, the more students’ records will be stored.
You can understand this from the program given below-
Example of class in C++
In the below codes, we declared two variables x and y
#include<iostream>
using namespace std;
class student
{
// private by default
int roll_no;
char name[20];
public: // public member
void get_record(void);
void put_record(void);
}; class closed
//input record from the user
void student::get_record()
{
cout<<"Enter roll no: ";
cin>>roll_no; cout<<"Enter Name : ";
gets(name);
}
//display record
void student::put_record()
{
cout<<"\nRoll no: "<<roll_no;
cout<<"\nName : "<<name;
}
// main program start
int main()
{
student x, y; // two variable declared
x.get_record();
y.get_record();
x.put_record();
y.put_record();
return 0;
}
OUTPUT
Enter roll no: 11
Enter Name : Rahul sherma
Enter roll no: 12
Enter Name : Anand sherma
Roll no: 11
Name : Rahul sherma
Roll no: 12
Name : Anand sherma
as you can see, we have stored the record of two students from this program.
define of class members in C++
The private member of a class is defined within the class itself, whereas the public member can be defined in two ways –
- inside the class
- outside the class
definition inside the class
without giving the function-declaration of members, direct given its definition inside the class. this type of definition also call inline definition.
syntax
class class-name
{
private:
..........
public:
return_type function_name()
{
body of function;
}
...........
};
definition outside The class
In this, the declaration of function-members is inside the class while their definition is defined outside the class by scope resolution operator. it is also called outline definition in C++
syntax
class class-name
{
private;
..............
public:
return-type function-name();
..........;
};
class-name :: return_type function_name()
{
body of function;
}
these two methods are given in the above program.
Difference between class and structure in C++
-
-
- all members of the structure are accessed by structure-variable whereas only public member in class can be accessed by class-variable.
- all members of the structure are declared publicly by default, whereas members of a class by default are private.
- class is an OOPs’ feature while the structure is taken from the process-oriented programming (POPs)
- the class has features- data-abstraction, data-encapsulation and inheritance while in the structure are not possible.
-
similarities between class and structure in C++
-
- like structure, the class name is also an identifier ie any unique name.
- both have members, data-type and function type.
- both do not reserve space in the memory until their variable or object declared.
- just as a member of a structure is accessed by a structure-variable, the class member (public member) is accessed from the class-variable or object.
- members of both class and structure are declared within a bracket and terminated by a semicolon.
- more about class and object,
- array of object in C++
Previous- what is OOPs in C++
Next- Friend function in C++