Chap 3 (Encapsulation)
Chap 3 (Encapsulation)
ENCAPSULATION
Contents
Private
Functions overloading
Constructors
Types of constructors
Destructors
Define Constructor From Outside Class
Friend function
Encapsulation
The wrapping up of data and functions together in a single
unit is known as encapsulation. It can be achieved by
making the data members' scope private and the member
function’s scope public to access these data members.
Encapsulation makes the data non-accessible to the
outside world.
Private
Private data can only be accessed from
within the class.
Private data can used to hidden data
Simple program of private
#include <iostream>
class company
private:
string name;
int id;
int salary;
public:
void add(string n, int i, int sal) // calling function for private data
{
name = n;
id = i;
salary = sal;
}
void print() // display function
{
cout << "---------------"<<endl;
cout << " your name is = " << name << endl;
cout << "your id is = " << id << endl;
cout << " your salary is = " << salary << endl;
}
};//End the class
int main()
{
company ob,ob1;//created two object
ob.add("ahmed", 123, 25678);
ob.print();
ob1.add("hamed1", 23, 28);
ob1.print();
return 0;
}
Simple program of private by using cin
#include <iostream>
class car
private:
string name;
int id;
int model;
public:
void call() //define function
{
cout << "enter your name ";
cin >> name;
cout << " enter your id ";
cin >> id;
cout << " enter your model ";
cin >> model;
}
};
int main()
{
car ob;
ob.call();
}
Overloading function
Function overloading is a feature of object-
oriented programming where two or more
functions can have the same name but different
parameters.
Simple program of overloading(inside class)
#include <iostream>
using namespace std;
class school
{
private:
string name;
int level;
int id;
public:
void print(string n, int I) // overloading function 1
{
name = n;
level = I;
cout << " your name is = " <<name<< endl;
cout << "your level is = "<<level<<endl;
}
void print(int i) // overloading function 2
{
id= i;
cout << " your id is = " << id << endl;
}
};
int main()
{
school x;
x.print("Ahmed",52);
x.print(8);
}
O/P
class school
{
private:
string name;
int level;
int id;
public:
void print(string n, int i);
void print(int l);
};
void school:: print(string n, int i)
{
name = n;
level = i;
cout << name << endl;
cout << level << endl;
}
void school::print(int l)
{
level = l;
cout << level << endl;
}
int main()
{
school x;
x.print("ALI", 22);
x.print(1);
}
Constructors
• What is constructor?
A constructor is a member function of a class
which initializes objects of a class.
• Default Constructors.
• Parameterized Constructors
• Copy Constructors
Default Constructors
• It has no parameters.
Constructors
SIMPLE PROGRAM OF CONSTRUCTOR
#include <iostream>
class School
private:
string name;
int id;
int no;
public:
School() // Default constructor
{
name = "ali";
id = 12;
no = 123;
}
void show() // display function
{
cout << name << endl;
cout << id<<endl;
cout<< no << endl;
}
};
int main()
{
School ob;
ob.show();
}
o/p
ali
12
123
Define Constructor From (Outside Class)
#include <iostream>
x = 40;
y = 30;
}
int Cal::sum() // function define(following the class Cal)
{
return x + y; // to return value (int values)
}
int main()
{
Cal ob;
cout << ob.sum();
O/P
70
Parameterized Constructors:
It is possible to pass arguments to constructors.
class Test
private:
Int a, b;
public:
Test()// DEFAULT constructor
{
cout << " Hi i am constructor" << endl;
}
Test(int x, int y) // Parameterized Constructors
{
a = x;
b = y;
}
void show()
};
int main()
{
Test ob1;// obj od default cot..
Test ob(1,2);// obj of paramrt…
}
O/P
Hi i am constructor
Welcome =
A=1
B=2
A = -858993460
B = -858993460
Copy constructor
It’s used to declare and initialize an object from another object of the same class.
#include<iostream>
using namespace std;
class item
Private:
int num;
public:
{
num = 10;
}
item(int x) //parametraiz constructor
{
num = x; // copy constructor declaration and define
}
#include <iostream>
class Sample
private:
int a;
int b;
public:
Sample() // constructor
{
a = 15;
b = 10;
}
~Sample()// destructor function
{
cout << " bye" << endl;// the program of destructor is executed
}
void show()
{
cout << "A = " << a << endl;
cout << "B = " << b << endl
}
};
int main()
{
Sample ob;
ob.show();
ob.~Sample();// calling destructor function
}
O/P
A = 15
B = 10
bye
bye
Exercise program for default & parametriz cons
//class cal
// int n1,n2
// default constructo to set the values by cin
// parameterized const to set the values by programmer
// 4 functions
// sum() to add n1,2
// divide() to divide n1,n2 and the result must be in decimal 1 / 2= 0.5
// mod() to modulus n1,n2 %
// mult() to multiply n1,n2
// two object in int main
#include<iostream>
using namespace std;
class cal
{
private:
int n1, n2;
public:
cal() // default constructor
{
cout << " Enter number vale N1 : ";
cin >> n1;
cout << " Enter number vale N2 : ";
cin >> n2;
cal(int x, int y)// parametariz cons
{
n1 = x;
n2 = y;
}
int sum() // sum function
{
return n1 + n2;
}
float divide() // division function
{
return(float) n1 / n2;
}
int mod()// mod function
{
return n1 % n2;
}
int mult() // mult function
{
return n1 * n2;
}
};
int main()
{
cal ob1;
cout<<ob1.sum()<<endl;
cout<<ob1.divide()<<endl;
cout<<ob1.mod()<<endl;
cout<<ob1.mult()<<endl;
cal ob2(1,2);
cout << "___________" << endl;
cout << ob2.sum() << endl;
cout << ob2.divide() << endl;
cout << ob2.mod() << endl;
cout << ob2.mult() << endl;
}
O/P