Jntuk 2-1 Oopt C++ - Unit-2
Jntuk 2-1 Oopt C++ - Unit-2
What is a class? How can you define a class in C++? Explain with
an example.
Object Oriented Programming encapsulates data (attributes) and
functions (behavior) into packages called classes.
The class combines data and methods for manipulating that data
into one package. An object is said to be an instance of a class. A class is
a way to bind the data and its associated functions together. It allows
the data to be hidden from external use. When defining a class, we are
creating a new abstract data type that can be treated like any other
built-in data type.
The class declaration describes the type and scope of its members. The
class function definitions describe how the class functions are
implemented. The general form of a class declaration is:
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
The data hiding is the key feature of OOP. By default, the members
are private. The variables declared inside the class are known as
data members and the functions are known as member functions.
The binding of data and functions together into a single class type
variable is referred to as encapsulation.
item x, y, *z;
Note that class specification provides only a template and does not
create any memory space for the objects.
2
www.jntufastupdates.com
How can you access the class members?
The object can access the public class members of a class by using dot
operator or arrow operator. The syntax is as follows;
class Test
{
public:
int x, y; // variables declaration
void show()
{
cout<<x<<y;
}
};
int main()
{
Test t; //Object creation
t.x = 10;
t.y = 20;
t.show();
};
Now, it display 10 and 20
3
www.jntufastupdates.com
or functions by the object. It is the default access. The keyword
private followed by colon (:) is used to make data member and member
function visible with in the class only.
int main()
{
Test t; //Object creation
t.x = 10;
t.y = 20;
};
4
www.jntufastupdates.com
void print()
{
cout<<”rno=”<<rno;
cout<<”name=”<<sname;
}
void read(int a, char *s)
{
rno = a;
strcpy(snm, s);
}
};
void main()
{ Output:
student s; rno= 10
s.read(10, “Rama”); name = Rama
s.print();
}
5
www.jntufastupdates.com
void student :: read (int a, char *s)
{
no = a;
strcpy(sname, s);
}
void student :: print()
{
cout<<”rno=”<<rno;
cout<<”name=”<<sname;
}
void main()
{ Output:
student s; rno= 10
s.read(10, “Rama”); name = Rama
s.print();
}
What are the characteristics of member functions?
The member functions are accessed only by using the object of the
same class.
The same function can be used in any number of classes.
The private data or private functions can be accessed only
by public member functions.
General form:
inline Return-type classname :: function-name (list of
arguments)
{
//function body
}
Example:
class student
{
private:
int rno;
char *sname;
6
www.jntufastupdates.com
public:
void print();
void read(int a, char *s)
{
no = a;
strcpy(snm, s);
}
};
inline void student :: print()
{
cout<<”no=”<<no;
cout<<”name=”<<snm;
}
void main()
Output:
{ rno= 10
student s; name = Rama
s.read(10, “Rama”);
s.print();
}
Example:
class ex
{
private:
int p;
protected:
int q;
public:
int r;
void getp()
7
www.jntufastupdates.com
{
p=10;
cout<<”private=”<<p;
}
void getq()
{
q=10;
cout<<”protected=”<<q;
}
void getr()
{
r=10;
cout<<”public =”<<r;
}
};
void main()
{
ex e; Output:
private =10
e.getp(); protected=10
e.getq(); public =10
e.getr(); public =100
e.r=100;
e.getr();
}
8
www.jntufastupdates.com
int absv:: num(int i)
{
return (abs(i));
}
double absv:: num( double d)
{
return (fabs( d));
}
void main()
{
absv a;
cout<<”\nThe absolute value of - 10 is “<<n. num(-10);
cout<<”\nThe absolute value of - 12.35 is “<<n.num(- 12.35);
}
Output:
The absolute value of -10 is 10
The absolute value of -12.35 is 12.35
int main(void)
{
A::B x;
x.show();
}
Output:
C++ is wonderful language
9
www.jntufastupdates.com
What is a constructor? What is a destructor? What are their
properties?
A constructor is a special member function used for automatic
initialization of an object. Whenever an object is created, the constructor
is called automatically. Constructors can be overloaded. Destructor
destroys the object. Constructors and destructors having the same name
as class, but destructor is preceded by a tilde (~) operator. The
destructor is automatically executed whenever the object goes out of
scope.
Characteristics of Constructors
Constructors have the same name as that of the class they belongs
to.
Constructors must be declared in public section.
They automatically execute whenever an object is created.
Constructors will not have any return type even void.
Constructors will not return any values.
The main function of constructor is to initialize objects and
allocation of memory to the objects.
Constructors can be called explicitly.
Constructors can be overloaded.
A constructor without any arguments is called as default
constructor.
Example:
class num
{
int a, b;
public:
num()
{
a =5; b=2;
}
void show()
{
cout<<a<<b;
}
};
10
www.jntufastupdates.com
Void main()
{ Output:
num n; 52
n.show();
}
Example:
class num
{
int a, b ,c;
public: Output:
num(int x, int y) 10 20
1 2
{
a=x; b=y;
}
void show()
{
cout<<a<<b;
}
};
void main()
{
num n(10,20);
//implicit call
n.show();
num x= num(1, 2);
//explicit call x.show();
}
Add( ) ; // No arguments
Add (int, int) ; // Two arguments
Example:
class num
{
int a, b;
public:
num() // Default constructor
{
a =10; b=20;
}
num(int x, int y) // Parameterized constructor
{
a=x;
b=y;
}
void add()
{
cout<<a+b;
}
};
void main()
{
num n; Output:
30
n.add();
3
num x(1,2);
x.add();
}
12
www.jntufastupdates.com
Whenever the object “x” is created the parameterized constructor is
executed automatically and a, and b values are assigned with 1 and 2
respectively.
Example:
class power
{
int b,p;
public:
power(int n=2, int m=3)
{
b=n;
p=m;
cout<<pow(n, m);
}
};
void main()
{ Output:
power x; 8 125 81
Power y(5);
power z(3,
4);
Characteristics of Destructors
Their name is the same as the class name but is preceded by a
tilde(~).
They do not have return types, not even void and they cannot
return values.
Only one destructor can be defined in the class.
Destructor neither has default values nor can be overloaded.
We cannot refer to their addresses.
An object with a constructor or destructor cannot be used as a
member of a union.
14
www.jntufastupdates.com
They make “implicit calls‟ to the operators new and delete when
memory allocation/ memory de-allocation is required.
Example:
class Test
{
public:
Test())
{ cout<<“\n Constructor Called”;
}
~Test()
{
cout<<“\n Destructor Called”;
}
}; Output:
void main() Constructor Called
{ Destructor Called
Test t;
}
15
www.jntufastupdates.com
class noname
{
int x;
public:
noname()
{
cout<<“\n In Default Constructor”;
x=10;
cout<<x;
}
noname(int i)
{
cout<<“\n In Parameterized Constructor”;
x=i;
cout<<x;
}
~noname()
{
cout <<“\n In Destructor”;
}
};
void main()
{
noname();
noname(12);
}
Output:
In Default Constructor 10
In Parameterized
Constructor 12 In
Destructor
In Destructor
16
www.jntufastupdates.com