0% found this document useful (0 votes)
37 views48 pages

Chap 3 (Encapsulation)

Chapters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views48 pages

Chap 3 (Encapsulation)

Chapters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

CHAPTER 3

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>

using namespace std;

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>

using namespace std;

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

your name is = Ahmed


your level is = 52
your id is = 8
EXECUTE FROM OUTSIDE CLASS
#include <iostream>
using namespace std ;

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.

• In C++, Constructor is automatically called when


object(instance of class) create.

• It is special member function of the class.

• A constructor is different from normal functions in


following ways:
Constructor has same name as the class itself

Constructors don’t have return type

If we do not specify a constructor, C++ compiler

generates a default constructor for us (expects no

parameters and has an empty body).


Types of Constructors

• Default Constructors.

• Parameterized Constructors

• Copy Constructors
Default Constructors

• Default Constructors is the constructor which

doesn’t take any argument.

• It has no parameters.

• C++ program to illustrate the concept of

Constructors
SIMPLE PROGRAM OF CONSTRUCTOR
#include <iostream>

using namespace std;

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>

using namespace std;


class Cal
{
private:
int x;
int y;
public:
Cal();// constructor

int sum(); // function of sum


};
Cal::Cal() // define constructor followig the calss Cal
{

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.

Typically, these arguments help initialize an object when it


is created.

To create a parameterized constructor, simply add


parameters to it the way you would to any other function.

When you define the constructor’s body, use the


parameters to initialize the object.

CPP program to illustrate parameterized constructors


• Uses of Parameterized constructor:
• It is used to initialize the various data elements of different objects with
different values when they are created.
• It is used to overload constructors.

• Can we have more than one constructors in a class?


Yes, It is called Constructor Overloading.
#include <iostream>

using namespace std;

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()

cout << "A = " << a << endl;

cout << "B = " << b << endl;

};
int main()
{
Test ob1;// obj od default cot..
Test ob(1,2);// obj of paramrt…

ob.show();// display of paramet


ob1.show();// display of default cons
return 0;

}
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:

item() //default constructor

{
num = 10;
}
item(int x) //parametraiz constructor
{
num = x; // copy constructor declaration and define
}

void display()//display function


{
cout << "\n Number is : " << num;
}
int main()
{
item a;
item b;
item c;
a.display();
b.display();
c.display();
}
O/P
Number is : 10
Number is : 10
Number is : 10
What is destructor?
Destructor is a member function which destructs or deletes an object.

• When is destructor called?


A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
How destructors are different from a normal member function?
Destructors have same name as the class preceded by a tilde (~)
Destructors don’t take any argument and don’t return anything
• Can there be more than one destructor in a class?
No, there can only one destructor in a class with class name preceded by ~,
no parameters and no return type.

• When do we need to write a user-defined destructor?


If we do not write our own destructor in class, compiler creates a default
destructor for us. The default destructor works fine unless we have
dynamically allocated memory or pointer in class. When a class contains a
pointer to memory allocated in class, we should write a destructor to release
memory before the class instance is destroyed. This must be done to avoid
memory leak.
Can a destructor be virtual?
Yes, In fact, it is always a good idea to make
destructors virtual in base class when we have a virtual
function.
#include <iostream>

#include <iostream>

using namespace std;

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

Enter number vale N1 : 1


Enter number vale N2 : 2
3
0.5
1
2
___________
3
0.5
1
2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy