Академический Документы
Профессиональный Документы
Культура Документы
Kartheek
UNIT-II
Syllabus:-
C++ Classes and Data Abstraction:
➔ Class definition,
➔ Class structure,
➔ Class objects,
➔ Class scope,
➔ this pointer,
➔ Friends to a class,
➔ Static class members,
➔ Constant member functions,
➔ Constructors and Destructors,
➔ Dynamic creation and destruction of objects,
➔ Data abstraction,
➔ ADT and information hiding.
1
Prepared by V.L.Kartheek
Introduction:-
➔ The main purpose of C++ programming is to add object orientation to the C programming
language and classes are the central feature of C++ that supports object-oriented programming
and are often called user-defined types.
Class Definition:-
Class class_name
{
Data Members;
Methods;
}
Example:
class A
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
}
We declare objects of a class with exactly the same sort of declaration that we declare variables
of basic types.
Syntax:-
classname objectname;
Example:-
2
Prepared by V.L.Kartheek
Class scope:-
The potential scope of a name declared in a class begins at the point of declaration and
includes the rest of the class body and all function bodies (even if defined outside the class
definition or before the declaration of the name), default arguments, exception specifications,
in-class brace-or-equal initializers, and all these things in nested classes, recursively.
Example:
#include <iostream>
#include<conio.h>
class rectangle //Declaration of Structure
{
int width, height;
};
int main(void)
{
class rectangle rec;
rec.width=8; //Initialization of member variables
rec.height=5;
cout<<"Width is: "<<rec.width<<endl<<“Heightis: "<<rec.height<<endl;
//Display contents of member variables
return 0;
}
Output:-
#include<iostream>
using namespace std;
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print() { cout << "x = " << x << endl; }
};
3
Prepared by V.L.Kartheek
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
this pointer:-
➔ Every object in C++ has access to its own address through an important pointer
called this pointer.
➔ The this pointer is an implicit parameter to all member functions. Therefore, inside a
member function, this may be used to refer to the invoking object.
➔ Friend functions do not have a this pointer, because friends are not members of a
class. Only member functions have a this pointer.
include<iostream>
using namespace std;
x=20
4
Prepared by V.L.Kartheek
Friends to a class:-
➔ We can also make a class a friend of another class. In that case, all the member function
of the class declared as friend become the friend functions of the other class.
Syntax:-
class A
{
friend class B;
};
class B
{
};
Example program:-
#include<iostream.h>
class Rectangle
{
int L,B;
public:
Rectangle()
{
L=10;
B=20;
}
friend class Square;
};
class Square
{
int S;
public:
Square()
{
S=5;
}
5
Prepared by V.L.Kartheek
Output :-
Length : 10
Breadth : 20
Side : 5
Syntax:-
static variabledefintion;
Example:-
static int c;
Example program:-
#include <iostream>
using namespace std;
class number
{
Static int c;
Public:
Void count()
{
++c;
Cout<<”\n c=”<<c;
}
};
//initialization of static member variable
Int number :: c=0;
Int main()
{
Number a,b,c;
a.count();
b.count();
c.count();
return 0;
}
OUTPUT:-
C=1
C=2
C=3
6
Prepared by V.L.Kartheek
➔ The const member functions are the functions which are declared as constant in the
program.
➔ The object called by these functions cannot be modified.
➔ It is recommended to use const keyword so that accidental changes to object are
avoided.
Example program:-
#include<iostream>
using namespace std;
class Demo
{
int val;
public:
Demo(int x = 0)
{
val = x;
}
int getValue() const
{
return val;
}
};
int main()
{
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}
OUTPUT:-
7
Prepared by V.L.Kartheek
Syntax:-
class classname
{
public:
classname(); //constructor declaration.
};
classname::classname() //constructor definition
{
------------
}
Example:-
#include<iostream>
using namespace std;
class Cube
{
public:
int side;
Cube()
{
side = 10;
}
};
int main()
{
Cube c;
cout << c.side;
}
OUTPUT:-
10
8
Prepared by V.L.Kartheek
DESTRUCTORS:-
➔ Are functions that are complimentary to constructors
➔ They deinitailize objects when they are destroyed.
➔ Invoked when an object of the class goes out of scope. (Or)
➔ The memory occupied by it is deallocated using the delete operator.
Rules:-
1.The name of destructor must be the same as the class but is pre-fixed with a ~ (tilde).
2. It is declared with no return type (not even void)
3. Should be declared in the public section within a class.
4. A class can have only destructor.
Syntax:-
class classname
{
public:
~classname(); //destructor declaration.
};
classname:: ~classname() //destructor definition
{
------------
}
Example:-
#include <iostream>
using namespace std;
class ABC
{
public:
ABC () //constructor defined
{
cout << "Hey look I am in constructor" << endl;
}
~ABC() //destructor defined
{
cout << "Hey look I am in destructor" << endl;
}
};
int main()
{
ABC cc1; //constructor is called
cout << "function main is terminating...." << endl;
/*....object cc1 goes out of scope, now destructor is being called...*/
return 0;
}
OUTPUT:-
Hey look I am in constructor
Function main is terminating....
Hey look I am in destructor
9
Prepared by V.L.Kartheek
➔ The new opearator returns the address of the object created,and it is stored in the
pointer ptr.
➔ A dynamic object can be destroyed using new operator as follows:
delete ptr;
#include<iostream.h>
class data
{
int x,y;
public:
data()
{
Cout<<”\n constructor”;
x=10;
y=50;
}
~data()
{
Cout<<”\n Destructor”;
}
void display()
{
Cout<<”\n x=”<<x;
Cout<<”\n y=”<<y;
}
};
void main()
{
data *d; // declaration of object pointer
d=new data; // dynamic object
d->display();
delete d; //deleting dynamic object
10
Prepared by V.L.Kartheek
OUTPUT:-
Constructor
x=10
y=50
Destructor
Data abstraction:-
➔ Data abstraction refers to providing only essential information about the data to the
outside world, hiding the background details or implementation.
➔ You can see in the below program, we are not allowed to access the variables a and b
directly, because a and b are hidden using private access specifier .
➔ To access those hidden data, you must call a public member function
Information hiding:-
Data hiding is hiding the details of internal data members of an object.
11
Prepared by V.L.Kartheek
public:
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
OUTPUT:-
a = 10
b = 20
12