Unit-Ii Notes
Unit-Ii Notes
Unit-Ii Notes
class class_name
public:
Ex:
Class stack
int stck[SIZE];
int tos;
public:
void init();
void push(int i);
int pop();
};
stck[tos]=i;
tos++;
}
- The :: is called the scope resolution operator.
- It tells the compiler that this version of push() belongs to the
stack class.
- Or, this push() is in stacks scope.
- In C++, several different classes can use the same function
same. The compiler knows which function belongs to which
class because of the scope resolution operator.
- When you refer to member of a class from a piece of code that
is not part of the calss, you must always do so in conjunction
with an object of that class to do so,
stack stack1,stack2;
stack1.init();
tos=0;
int stack::pop()
if(tos==0){
return 0;
tos--;
return stck[tos];
int main()
stack1.init();
stack2.init();
stack1.push(1);
stack2.push(2);
stack1.push(3);
stack2.push(4);
cout<<stack1.pop()<<” ”;
cout<<stack1.pop()<<” ”;
cout<<stack2.pop()<<” ”;
cout<<stack2.pop()<<” ”;
return 0;
Output:
3142
i. #include<iostream>
class test
{
int a,b;
public:
void show()
a=10;
b=10;
cout<<”a=”<<this->a<<endl;
cout<<”b=”<<this->b<<endl;
};
int main()
test t;
t.show();
return 0;
#include<iostrem>
int a,b;
public:
a=a;
b=b;
void display()
cout<<a<<endl;
cout<<b;
};
int main()
test t;
t.show(10,20);
t.display();
}
b=b;
this->a=a; or (*this).a=a;
this->b=b; or (*this).b=b;
Friend classes:
- It is possible for one class to be a friend of another class.
- When this is the case, the friend class & all of its functions have
access to the private members defined within the other class.
- For ex.
- // using friend class
#include<iostream>
using namespace std;
class TwoValues
{
int a;
int b;
public:
TwoValues(int i,int j){
a=i;
b=j;
}
friend class Min;
};
class Min
{
public:
int min(TwoValues x);
};
int Min::min(TwoValues x)
return x.a<x.b?x.a:x.b;
int main()
TwoValues ob(10,20);
Min m;
cout<<m.min(ob);
return 0;
Output:
10
- In this ex., class Min has access to the private variables a & b
declared within the TwoValues class.
- It is critical to understand that when one class is a friend of
another, it only has access names defined within the other
class. It does not inherit the other class.
- Specifically , the members of the first class do not become
members of the friend class.
void getcount(void)
cout<<”count”;
count<<count<<”\n”;
};
int item::count;
int main()
{
item a,b,c;
void showcode(void)
cout<<”object number:”<<code<<”\n”;
cout<<”count:”<<count<<”\n”;
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
Output:
Count:2
Count:3
Object number:1
Object number:2
Object number:3
Introduction:
Constructor:
{
m=0;
n=0;
Parameterized Constructor:
Intger::integer(int x,int y)
m=x;
n=y;
- Here,
integer int1; //doesn’t works
- We have to pass arguments, to the constructor. This can be
done in two ways.
(i). By calling constructor explicitly
integer int1=integer(0,100);
(ii). By calling the constructor implicitly
integer int1(0,100);
Ex. This program defines a called point that stores the x & y
coordinates of a point.
#include<iotsream>
class point
{
int x;
int y;
public:
point(int a,int b)
{
x=a;
y=b;
}
void display()
Cout<<”C”<<x<<”,”<<y<<”)\n”;
};
int main()
point p2(5,10);
cout<<”Point p1=”;
p1.display();
cout<<”Point p2=”;
p2.display();
return 0;
Output:
Point p1=(1,1)
Point p2=(5,10)
integer(int a,int b)
m=a;
n=b;
integer(integer &i)
m=i.m;
n=i.n;
};
int main()
{
integer I1;
integer I2(20,40);
integer I3(I2);
return 0;
Copy constructor:
Integer I2=I1;
- integer(integer &i);
#include<iostream>
using namespace std;
class code
{
int id;
public:
code()
{
}
code(int a)
id=a;
id=x.id;
void display()
cout<<id;
};
int main()
{
cout<<”\n id of A:”;
A.display();
cout<<”\n id of B”;
B.display();
cout<<”\n id of C”;
C.display();
cout<<”\n id of D”;
D.display();
return 0;
Output:
Id of A:100
Id of B:100
Id of C:100
Id of D:100
Destructor:
- it is used to destroy the objects that have been created by a
constructor.
- Like constructor, the destructor is a member function whose
name is the same as the class name but is preceded by a tilde.
- For ex. The destructor for the class integer can be defined as
shown below,
~integer()
{
}
- A destructor never takes any argument nor does it return value.
- It will be invoked implicitly by the compiler upon exist from the
program(or block or function etc).
- The ex. Below illustrates that the destructor has been invoked
implicitly by the compiler.
#include<iostream>
using namespace std;
int count=0;
class test
{
public:
test()
count++;
cout<<”\n Constructor Msg:object number”;
~test()
{
cout<<”\n Destructor Msg:object
number”<<count<<”destroyed”;
count—;
};
int main()
test T1;
test T2,T3;
cout<<”\n\nLeaving Block1..”;
return 0;
Output:
Inside block 1
Leaving Block1..
~data()
cout<<”\n Destructor ”;
void display()
{
cout<<”\n x=”<<x;
cout<<”\n y=”<<y;
};
int main()
d->display();
return 0;
Output:
Constructor
x=10
y=50
Destructor
Data Abstraction:
- Data Abstraction is a process of providing only the essential details
to the outside world and hiding the internal details, i.e.,
representing only the essential details in the program.
- Data Abstraction is a programming technique that depends on the
seperation of the interface and implementation details of the
program.
- Let's take a real life example of AC, which can be turned ON or OFF,
change the temperature, change the mode, and other external
components such as fan, swing. But, we don't know the internal
details of the AC, i.e., how it works internally. Thus, we can say that
AC seperates the implementation details from the external
interface.
- C++ provides a great level of abstraction. For example, pow()
function is used to calculate the power of a number without knowing
the algorithm the function follows.
- Data Abstraction is a process of providing only the essential details
to the outside world and hiding the internal details, i.e.,
representing only the essential details in the program.
- Data Abstraction is a programming technique that depends on the
seperation of the interface and implementation details of the
program.
- Let's take a real life example of AC, which can be turned ON or OFF,
change the temperature, change the mode, and other external
components such as fan, swing. But, we don't know the internal
details of the AC, i.e., how it works internally. Thus, we can say that
AC seperates the implementation details from the external
interface.
An abstraction can be achieved using classes. A class is used to group all the
data members and member functions into a single unit by using the access
specifiers. A class has the responsibility to determine which data member is to
be visible outside and which is not.
Abstraction in header files:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int n = 4;
int power = 3;
int result = pow(n,power); // pow(n,power) is the power function
cout << "Cube of n is : " <<result<< endl;
return 0;
}
Output:
Cube of n is : 64
In the above example, pow() function is used to calculate 4 raised to the
power 3. The pow() function is present in the math.h header file in which
all the implementation details of the pow() function is hidden.
#include <iostream>
using namespace std;
class Sum
{
private:
int x, y, z; // private variables
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}
Output:
Advantages Of Abstraction:
o Implementation details of the class are protected from the
inadvertent user level errors.
o A programmer does not need to write the low level code.
o Data Abstraction avoids the code duplication, i.e., programmer does
not have to undergo the same tasks every time to perform the
similar operation.
o The main aim of the data abstraction is to reuse the code and the
proper partitioning of the code across the classes.
o Internal implementation can be changed without affecting the user
level code.
- When you start your car, you don't need to know the intricate
workings of the starter motor. All you need to do is turn the key to
initiate the sequence.
- If successful, the engine will turn over. This real-world example
highlights the programming concept of data abstraction, which
allows a programmer to protect/hide the implementation of a
process and only gives the keys to other functions or users.
- You only need to know enough about a given function to run it but
don't need to know (or care) about how the internal code works.
- An abstract data type (or ADT) is a class that has a defined set of
operations and values.
- In other words, you can create the starter motor as an entire
abstract data type, protecting all of the inner code from the user.
When the user wants to start the car, they can just execute the
start() function.
- In programming, an ADT has the following features:
- An ADT is a prime example of how you can make full use of data
abstraction and data hiding.
- This means that an abstract data type is a huge component of
object-oriented programming methodologies: enforcing abstraction,
allowing data and encapsulation.
One of the most common ADTs is the stack. Let's take a look at it in
action.
#include <iostream>
class Stack {
public:
int top;
int size[MAX_SIZE];
//constructor
Stack() {
top = -1;
//function declarations
int pop();
bool is_empty();
};
//are we empty?
bool Stack::is_empty() {
int Stack::pop() {
if(top < 0) {
return 0;
} else {
return page;
return false;
} else {
size[++top] = page;
return true;
int main( ) {
//new stack
Stack pages;
pages.push(5);
pages.push(10);
pages.push(15);
pages.push(20);
cout << " Page " << pages.pop() << " popped from stack " << endl;
cout << " Page " << pages.pop() << " popped from stack " << endl;
return 0;
Output:
-queue
Information Hiding: