C++ Program to access public data member.
#include<iostream>
using namespace std;
class student
{
public:
int rollno;
};
int main()
{
student a;
student b;
a.rollno=101;
b.rollno=102;
cout<<"Roll no of A is"<< a.rollno;
cout<<"Roll no of B is"<< b.rollno;
return 0;
}
C++ Program to access private data member.
#include<iostream>
using namespace std;
class student
{
private:
int rollno;
public:
void readdata(int rollno)
{
rollno=rn;
}
void show()
{
cout<< rollno;
}
};
int main()
{
student a, b;
a.readdata(101);
b.readdata(102);
cout<<"Roll no of A is";
a.show();
cout<<"Roll no of B is";
b.show();
return 0;
}
C++ Program to illustrate the working of objects and class .
#include<iostream>
using namespace std;
class Student
{
public:
int id;
string name;
};
int main()
{
Student s1;
s1.id = 201;
s1.name = "jpwebdevelopers";
cout<< s1.id<< endl;
cout<< s1.name<< endl;
return 0;
}
C++ Program to define member function inside the class defination.
#include<iostream>
using namespace std;
class employee
{
int empid,salary;
public:
void display()
{
empid=101;
salary=10000;
cout<<"employee id is"<< empid << endl;
cout<<"employee salary is"<< salary;
}
};
int main()
{
employee e;
e.display();
return 0;
}
C++ Program to define member function outside the class defination.
#include<iostream>
using namespace std;
class employee
{
int empid,salary;
public:
void display();
};
void employee::display()
{
empid=101;
salary=10000;
cout<<"employee id is"<< empid << endl;
cout<<"employee salary is"<< salary ;
}
int main()
{
employee e;
e.display();
return 0;
}
C++ Program to for inline function.
#include<iostream>
using namespace std;
inline int addnum(int x,int y)
{
return (x+y);
}
int main()
{
int a=5;
int b=10;
cout<< addnum(a,b);
return 0;
}
C++ Program to for Static function.
#include<iostream>
using namespace std;
class employee
{
static int empid,salary;
public:
static void display()
{
cout<<"employee id is"<< empid << endl;
cout<<"employee salary is"<< b.rollno;
}
void employee::display()
{
empid=101;
salary=10000;
cout<<"employee id is"<< empid << endl;
cout<<"employee salary is"<< salary;
}
};
int employee :: empid=101;
int employee :: salary=2000;
int main()
{
employee e;
cout<<"Printing through object name";
e.display();
cout<<"Printing through class name";
employee ::display();
return 0;
}
C++ Program to for Friend function.
#include<iostream>
using namespace std;
class add
{
int a, b;
public:
void input()
{
cout<<"Enter the value of a and b";
cin>>a>>b;
}
friend void sum(add obj);
};
void sum(add obj)
{
int c;
c=obj.a+obj.b;
cout<<"Sum is = "<< c;
}
int main()
{
add aobj;
aobj.input();
sum(aobj);
return 0;
}