Classes in C++

Classes in C++ programming is a building block that leads to OOP. It is user-defined data type which combines data and methods for manipulating that data into one package.

Access Modifier

Probably the most influential set of access modifiers started with C++, which has three.

Access Modifier Description
public Any class can access the features
protected Any subclass can access the feature
private No other class can access the feature

A class can also give access to another class or method with the friend keyword.


Class Program

Program

#include<iostream.h>
#include<conio.h>
void main()
{
class cls //'cls' is name of Class.
{
public: //using Functions as a Public.
void fun() //this is function.
{
cout<<"This is fun Function"<<endl;
}
void fun2() //this is function.
{
cout<<"This is fun2 Function"<<endl;
}
};
cls obj; //'obj' is object of class 'cls'.
obj.fun(); //with the help of object 'obj' calling 'fun()' function.
obj.fun2(); //with the help of object 'obj' calling 'fun2()' function.
getch();
}
Output
This is fun Function
This is fun2 Function

Program Class Another Method

Program

#include<iostream.h>
#include<conio.h>
void main()
{
class cls //'cls' is name of Class.
{
public: //using Functions as a Public.
void fun() //this is function.
{
cout<<"This is fun Function"<<endl;
}
void fun2() //this is function.
{
cout<<"This is fun2 Function";
}
};
cls obj, obj2; //'obj' and 'obj2' is objects of Class 'cls'.
obj.fun(); //with the help of object 'obj' calling 'fun()' function.
obj2.fun2(); //with the help of object 'obj2' calling 'fun2()' function.
getch();
}
Output
This is fun Function
This is fun2 Function

Program Class Another Method
Program

#include<iostream.h>
#include<conio.h>
class cls //'cls' is name of Class.
{
public:
void fun() //this is function.
{
cout<<"This is fun Function"<<endl;
}
void fun2() //this is function.
{
cout<<"This is fun2 Function";
}
};
void main()
{
cls obj, obj2; //'obj' and 'obj2' is objects of Class 'cls'.
obj.fun(); //with the help of object 'obj' calling 'fun()' function.
obj2.fun2(); //with the help of object 'obj2' calling 'fun2()' function.
getch();
}
Output
This is fun Function
This is fun2 Function