Oops
Oops
Oops
class MyClass {
public:
};
Example
class MyClass {
public: int x;
private:
int y;
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Example
class MyClass {
int x; // Private attribute
int y; // Private attribute
};
// access modifier
Program – 1 :
#include<iostream>
using namespace std;
class Circle
{
private: double radius;
// public member function
public: double compute_area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle obj;
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
}
Output:
double radius;
obj.radius = 1.5;
^
The output of the above program is a compile time error because we are
not allowed to access the private data members of a class directly from
outside the class. Yet an access to obj.radius is attempted, but radius
being a private data member, we obtained the above compilation error.
Program – 2 :
#include<iostream>
class Circle
radius = r;
}
};
int main()
Circle obj;
obj.compute_area(1.5);
Output:
#include <iostream>
using namespace std;
{
class date {
public :
int day, month, year;
};
int main()
{
date today;
today.day = 10;
today.month = 5;
today.year = 2007;
cout << “ Today’s date is = ” << today.day << “/”; cout << today.month <<
“/” << today.year << endl; return 0;
}
Output of the above program
While the keyword public is not used to define the members of a class,
the C++ compiler assumes, by default, that all its members are private. The
data members are not accessible outside the class. For example, the
following program demonstrates the accessibility of the members.
Program 6
#include<iostream>
using namespace std;
int main()
{
class date { // by default, members are
private int day,month,year;
};
class date
today;
today.day = 10;
today.month = 5;
today.year = 2007;
A program to demonstrate how to define both data member and member function of a class within the
scope of class definition.
Program 7
#include<iostream>
int main()
{
date today;
today.getdata();
today.display();
}
Program 8
int main()
{
sample obj1;
obj1.getinfo();
obj1.display();
obj1.sum();
obj1.diff();
obj1.mult();
obj1.div();
return 0;
}
Program 11 WAP of Dereferencing Operator
#include <iostream.h>
using namespace std;
int main()
{
int a = 10, b = 20;
int* pt;
pt = &a;
cout << "The address where a is stored is: " << pt <<
endl;
cout << "The value stored at the address by
dereferencing the pointer is: << *pt << endl;
}
Output
The address where a is stored is: 0x7ffdcae26a0c
The value stored at the address by dereferencing the
pointer is: 10
Program 12: We Can Directly assign value to a
pointer using Dereference :
#include <iostream>
using namespace std;
int main() {
int *p;
*p = 80; //Assiging value to pointer p by using
asterisk
cout << *p<<endl;
return 0;
}
Output
80
In Above code we directly assign value to pointer p
using asterisk (*) and using dereference we print its
pointed value that is 80.
Program 13
// C++ Program to changing the address pointed by
pointer
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20;
int* pt;
// Referencing to the pointer
pt = &a;
cout << "The address where a is stored is: " << pt <<
endl;
Program 14
A program to demonstrate the concept of arrays as
class members
Example:
#include<iostream>
class student
{
private:
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};
class student
{
private:
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};
Program 17
// C++ program that declare array as
data members
#include<iostream>
using namespace std;
class Employee
{
int id;
char name[30];
public:
void getdata();//Declaration of
function
void putdata();//Declaration of
function
};
void Employee::getdata(){//Defining of
function
cout<<"Enter Id : ";
cin>>id;
cout<<"Enter Name : ";
cin>>name;
}
void Employee::putdata(){//Defining of
function
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
}
int main(){
Employee emp; //One member
emp.getdata();//Accessing the
function
emp.putdata();//Accessing the
function
return 0;
}
Program 18
// C++ program that declare array as
object.
#include<iostream>
using namespace std;
class Employee
{
int id;
string name[30];
public:
void getdata();
void putdata();
};
void Employee::getdata()
{
cout << "Enter Id : ";
cin >> id;
cout << "Enter Name : ";
cin >> name;
}
void Employee::putdata()
{
cout << id << " ";
cout << name << " ";
cout << endl;
}
int main()
{
Employee emp[30];
int n, i;
cout << "Enter Number of Employees
- ";
cin >> n;
for(i = 0; i < n; i++)
emp[i].getdata();
void getdata() {
cout << "Enter roll number:
"<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}
void putdata() {
cout<<"Roll Number = "<< rollNo
<<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks
<<endl;
cout<<endl;
}
};
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();
Student s2;
s2.getdata();
s2.putdata();
Student s3;
s3.getdata();
s3.putdata();
cout << "Total objects created = "
<< Student::objectCount << endl;
return 0;
}
Output
The output of the above program is as
follows −
Enter roll number: 1
Enter name: Mark
Enter marks: 78
Roll Number = 1
Name = Mark
Marks = 78
Program 20
// C++ program to show passing of objects to a
function
#include <iostream>
using namespace std;
class Demo {
private:
int a;
public:
void set(int x)
{
a = x;
}
void sum(Demo ob1, Demo ob2)
{
a = ob1.a + ob2.a;
}
void print()
{
cout << "Value of A : " << a << endl;
}
};
int main()
{
//object declarations
Demo d1;
Demo d2;
Demo d3;
//assigning values to the data member of objects
d1.set(10);
d2.set(20);
//passing object d1 and d2
d3.sum(d1, d2);
//printing the values
d1.print();
d2.print();
d3.print();
return 0;
}
Output
Value of A : 10
Value of A : 20
Value of A : 30
Program 21 :defining the constructor within the class
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};
int main()
{
student s;
}
Program 22:// Cpp program to illustrate the concept of
Constructors
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
cout<<a<<b;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 0;
}
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX()<< ", p1.y = " << p1.getY();
return 0;
}
Program 24: // C++ Program to demonstrate the copy
constructor
#include <iostream>
#include <string.h>
using namespace std;
class student {
int rno;
string name;
double fee;
public:
student(int, string, double);
student(student & t) // copy constructor
{
rno = t.rno;
name = t.name;
fee = t.fee;
}
void display();
};
Student :: student(int no, string n, double f)
{
rno = no;
name = n;
fee = f;
}
void student :: display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
int main()
{
student s (1001, "Ram", 10000);
s.display();
student obj (s); // copy constructor called
obj.display();
return 0;
}
Program 25
// C++ program to demonstrate the execution of constructor
// and destructor
#include <iostream>
using namespace std;
class Test {
public:
Test()
{ cout << "\n abc………….";
}
// Destructor
~ Test()
{ cout << "\nDestructor executed";
}
};
main()
{
Test t;
return 0;
}
Program 26
//C++ program to demonstrate the execution of constructor
and destructor when multiple objects are created
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test()
{ cout << "\n Constructor executed";
}
// User-Defined Destructor
~Test()
{ cout << "\n Destructor executed";
}
};
main()
{
// Create multiple objects of the Test class
Test t, t1, t2, t3;
return 0;
}
Program 27
A program to access the private data of a
class by non-member function through
friend function where the friend function
is declared in the location of public
category.
#include<iostream>
using namespace std;
class sample{
private : int x;
public : void getdata();
friend void display(class sample);
};
void sample :: getdata(){
cout<<"Enter a value for x\n*";
cin>>x;
}
void display(class sample abc)
{
cout<<"Entered number is : "<<abc.x;
cout<<endl;
}
int main()
{
sample obj;
obj.getdata();
cout<<"accessing the private data by
non-member ";
cout<<"function \n";
display (obj);
return 0;
}
Program 28
A program to access the private data of a
class by non-member function through
friend function where the friend function
is declared in the location of private
category.
//declaring friend function
#include<iostream>
using namespace std;
class sample{
private : int x;
friend void display(class sample);
public : void getdata();
};
#include<iostream>
using namespace std;
class second; //forward declaration
class first{
private : int x;
public : inline void getdata();
inline void display();
friend int sum(first, second);
};
class second{
private : int y;
public : inline void getdata();
inline void display();
friend int sum (first, second);
};
inline void first :: getdata(){
cout<<"Enter a value for x\n";
cin>>x;
}
inline void second :: getdata(){
cout<<"Enter a value for y\n";
cin>>y;
}
inline void first :: display()
{
cout<<"Entered number is (x) = ";
cout<<x<<endl;
}
inline void second :: display(){
cout<<"Entered number is (y) = ";
cout<<y<<endl;
}
int sum(first one, second two){
int temp;
temp = one.x + two.y;
return(temp);
}
int main()
{
first a;
second b;
a.getdata();
b.getdata();
a.display();
b.display();
int te = sum(a,b);
cout<<"sum of the two private data
variables (x+y)";
cout<<"sum of the two private data
variables (x + y)";
cout<<" = "<<te<<endl;
return 0;
}
Unit 4
Program 31 C++ program to illustrate Base &
Derived Class
#include <iostream>
using namespace std;
// Declare Base Class
class Base {
public:
int a;
};
Program 32
WAP to find the sum of 2 numbers using single
inheritance.
Program 33
WAP to demonstrate how a class can be inherit
Privetely.
#include <iostream>
using namespace std;
class Person {
public: int id;
string name ;
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
class Student : private Person {
string course;
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Program 34
WAP to find the average of marks of 2 students
by inheriting the classclass can be inherit
Privetely.This is a program of single inheritance.
Program 34
WAP to display name,is salary of an employee
by single inheritance and the class can be inherit
Privetely.This is a program of single inhertance
Program 35
WAP to find the product of 3 numbers and the
class can be inherit Publicly.This is a program of
multilevel inhertance
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_A()
{
cout<<"Enter the Value of A=";
cin>>a;
}
void disp_A()
{
cout<<endl<<"Value of
A="<<a;
}
};
class B: public A
{
private: int b;
public: void set_B()
{
cout<<"Enter the Value of B=";
cin>>b;
}
void disp_B()
{
cout<<endl<<"Value of B="<<b;
}
};
class C: public B
{
int c, p;
public:
void set_C()
{
cout<<"Enter the Value of C=";
cin>>c;
}
void disp_C()
{
cout<<endl<<"Value of C="<<c;
}
void cal_product()
{
p=a*b*c;
cout<<endl<<"Product of "<<a<<" *
"<<b<<" * "<<c<<" = "<<p;
}
};
main()
{
C objc ;
objc.set_A();
objc.set_B();
objc.set_C();
objc .disp_A();
objc. disp_B();
objc.disp_C();
objc.cal_product();
return 0;
Program 36
WAP to find the average of 5 numbers and the
class can be inherit Publicly.This is a program of
multilevel inheritance
Program 37
WAP to find the age of n students and it’s average
also ask marks of n students find average marks
and the class can be inherit Publicly.This is a
program of multiple inheritance
Program 38
#include <iostream>
using namespace std;
};
int main ()
{
result obj; // create an object of the derived class
class A
{
public:
void show_A() {
cout<<"class A"<<endl;
}
};
class B : public A
{
public:
void show_B() {
cout<<"class B"<<endl;
}
};
class C : public A
{
public:
void show_C() {
cout<<"class C"<<endl;
}
};
int main() {
B b; // b is object of class B
cout<<"calling from B: "<<endl;
b.show_B();
b.show_A();
C c; // c is object of class C
cout<<"calling from C: "<<endl;
c.show_C();
c.show_A();
return 0;
}
Program 40
Wap of Hierarchical inheritance to show a customer
bought 5 items from the grocery shop and 3 vegetable
from the vendor, find the total cost of items the
customer bought.
Program 41
// C++ program to illustrate the hybrid inheritance
Using Multilevel Inheritance and Hierarchical
Inheritance
#include <iostream>
using namespace std;
// Base class 1
class Meal {
public:
void print()
{
cout << "Different types of meals are served :"
<< endl;
}
};
int main()
{
Meal types;
Breakfast servedBreakfast;
Milk milk;
Yoghurt yoghurt;
Dessert servedDessert;
Sweets s;
Pastry pastry;
types.print();
servedBreakfast.print();
milk.print();
yoghurt.print();
servedDessert.print();
sweet.print();
pastry.print();
return 0;
}
Program 42
class A {
public:
void show()
{
cout << "Peter\n";
}
};
class B : public A {
};
class C : public A {
};
int main()
{
D object;
object.show();
}
Compile Errors:
prog.cpp: In function 'int main()':
prog.cpp: error: request for member 'show' is
ambiguous
object.show();
^
prog.cpp: note: candidates are: void A::show()
void show()
^
Program 43 Program to Demonstrate Virtual Function
#include <iostream>
using namespace std;
class A {
public:
int a;
show()
{
a = 10;
}
};
class B : public virtual A
{
};
int main()
{
D object;
cout << "a = " << object.show() << endl;
return 0;
}
Output
a = 10
Program 44
// C++ program to demonstrate function overloading or
Compile-time Polymorphism
#include <iostream>
using namespace std;
class Demo {
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}
void func(double x)
{
cout << "value of x is " << x << endl;
}
int main()
{
Demo obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85, 64);
return 0;
}
Output
value of x is 7
value of x is 9.132
value of x and y is 85, 64
Program 45
WAP To achieve compile time polymorphism via
function overloading and display your name,
rollno, jluid and course.
Program 46
#include <iostream>
using namespace std;
class base {
public:
virtual void print()
{ cout << "print base class\n";
}
void show()
{ cout << "show base class\n";
}
};
class derived : public base
{
public:
void print()
{ cout << "print derived class\n";
}
void show()
{ cout << "show derived class\n";
}
};
int main()
{
Base * bptr;
derived d;
bptr = &d;
return 0;
}
Output
print derived class
show base class
Program 47
WAP to demonstrate the setw & endl
manipulators.
Program 48
WAP to demonstrate the setfill manipulators.
Program 49
WAP to demonstrate the setprecision
manipulators.
Program 50
Program 51
WAP TO read the line from keyboard and display
the function as array to the screen using cin.get
function.