Вы находитесь на странице: 1из 12

Prepared by V.L.

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

C++ Classes and Data Abstraction

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

➔ A class is an abstract data type similar to ‘C structure‘.


➔ The Class representation of objects and the sets of operations that can be applied to
such objects.
➔ The class consists of Data members and methods.

Class structure Syntax:

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
}

Define C++ Objects:-

We declare objects of a class with exactly the same sort of declaration that we declare variables
of basic types.
Syntax:-

classname objectname;

Example:-

Box Box1; // Declare Box1 of type Box


Box Box2; // Declare Box2 of type Box

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;

/* 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; }
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
Output:

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

void Display(Rectangle Rect)


{
cout<<"\n\n\tLength : "<<Rect.L;
cout<<"\n\n\tBreadth : "<<Rect.B;
cout<<"\n\n\tSide : "<<S;
}
};
void main()
{
Rectangle R;
Square S;
S.Display(R);
}

5
Prepared by V.L.Kartheek

Output :-
Length : 10
Breadth : 20
Side : 5

Static class members:-

➔ A data member declared as static is a static data member.


➔ The static keyword is used to preserve the value of a variable.
➔ When a variable is declared as static, it is initialized with zero.

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

Constant member functions:-

➔ 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.

syntax of const member function in C++ language:-

datatype function_name() const;

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

The value using object d : 28


The value using object d1 : 8

7
Prepared by V.L.Kartheek

Constructors and Destructors:-

➔ A class in C++ may contain two special categories of member functions


(constructor& destructors) which are involved in the internal workings of the class.
CONSTRUCTOR:-
➔ A constructor is a special member function for automatic initialization of an object.
➔ Whenever an object is created the constructor will be executed automatically.
➔ Constructor is called special member function because it constructs members of the
class.
Rules:-
1. The name of constructor must be the same as that of its class.
2. It is declared with no return type (not even void)
3. Should be declared in the public section within a class.

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

Dynamic creation and destruction of objects:-

➔ An object can be created at runtime;such an object is called a dynamic object.


➔ The new and delete operators are used to allocate and deallocate memory to such
objects.
A dynamic object can be created using new operator as follows:

Ptr = new classname;

➔ 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;

Example Program to creation and destruction of dynamic Objects:-

#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

Abstract Data Types:-


➔ Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined
by a set of value and a set of operations.
➔ The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented.
➔ It is called “abstract” because it gives an implementation-independent view. The
process of providing only the essentials and hiding the details is known as
abstraction.
Examples:- List, Stack, Queue

Information hiding:-
Data hiding is hiding the details of internal data members of an object.

• Data hiding is also known as Information hiding.


• Sometimes Data Hiding includes Encapsulation. Thus Data Hiding is heavily related
to Abstraction and Encapsulation.

11
Prepared by V.L.Kartheek

Example program for Data abstraction (or) Information Hiding :-


#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;

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

Вам также может понравиться