C++ Programming Unit IV
C++ Programming Unit IV
Passing Objects as Arguments: In C++, an object is treated as a built in data type. An object
can be used as a function argument. It can be done in the following two ways:
(a) Pass by value: A copy of the object is passed to the function and any change made is not
reflected back. An object may be passed by value to a member function, a non member function and
a friend function.
Example
#include<iostream.h>
#include<conio.h>
class sum
{
private:
int a, b;
public:
void get_value( )
{
cout<<”\n Enter the Values:-“;
cin>>a>>b;
}
void add(sum s1, sum s2)
{
int x, y;
x = s1.a + s2.a;
y = s1.b + s2.b;
cout<<”\n Values after addition :-“;
cout<<”\nX =” <<x << ”\nY =” <<y;
}
};
void main( )
{
sum ob, ob1, ob2;
clrscr( );
ob1.get_value( );
ob2.get_value( );
ob.add(ob1, ob2);
getch( );
}
Example
#include<iostream.h>
#include<conio.h>
class sum
{
private:
int a, b, c;
public:
void get_value(sum &obj)
{
cout<<”\n Enter the Values:-“;
cin>>obj.a>>obj.b>>obj.c;
}
void add(sum &s1, sum &s2)
{
int x, y, z;
x = s1.a + s2.a;
y = s1.b + s2.b;
z = s1.c + s2.c;
cout<<”\n Values after addition :-“;
cout<<”\nX =” <<x;
cout<<”\nY =” <<y;
cout<<”\nZ =” <<z;
}
};
void main( )
{
sum ob, ob1, ob2;
clrscr( );
ob.get_value(ob1);
ob.get_value(ob2);
ob.add(ob1, ob2);
getch( );
}
Example:
#include<iostream.h>
#include<conio.h>
class plus
{
private:
int a, b, c;
public:
plus( int x, int y, int z)
{
a = x;
b = y;
c = z;
}
friend plus operator + (plus, plus);
void show( );
};
void plus::show( )
{
cout << “\n The value of a is :-“ << a;
cout << “\n The value of b is :-“ << b;
cout << “\n The value of c is :-“ << c;
}
void main( )
{
clrscr( );
plus ob(5, 15 ,17), ob1(2, 7, 9), ob2;
cout << “\n The values after plus operator are:-“
ob2 = ob + ob1;
ob2.show( );
getch( );
}
Example
#include<iostream.h>
#include<conio.h>
void main( )
{
show( ); // Call it like a normal function
getch( );
}
For accessing the data, the declaration of a friend function should be done inside the body of a class starting
with the keyword friend.
Syntax
class class_name
{
friend data_type function_name(arguments);
};
Example
#include<iostream.h>
#include<conio.h>
class Box
{
private:
int length;
public:
Box ( )
{
length = 0;
}
Abstract Class: Abstract Class is a class which contains atleast one Pure Virtual function in it.
Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class
must provide definition to the pure virtual function, otherwise they will also become abstract class.
1. Abstract class cannot be instantiated, but pointers and references of Abstract class type can be
created.
2. Abstract class can have normal functions and variables along with a pure virtual function.
3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will
become Abstract too.
Example
#include <iostream.h>
#include <conio.h>
class Shape
{
protected:
float l;
public:
void getData( )
{
cin >> l;
}
• Functions of both parent and child class must have the same name.
• Functions must have the same argument list and return type.
• A function declared static cannot be overridden.
• If a function cannot be inherited, it cannot be overridden.
In function overriding when an object of the derived class is created and it call the member function which
exists in both classes (base and derived), the member function of the derived class is invoked and the
function of the base class is ignored.
#include <iostream.h>
#include<conio.h>
class BaseClass
{
public:
void disp( )
{
cout<<"Function of Parent Class";
}
};
class DerivedClass: public BaseClass
{
public:
void disp( )
{
cout<<"Function of Child Class";
}
};
void main( )
{
DerivedClass obj;
obj.disp( );
getch( );
}
variables of
|
Constructors: A constructor is a ‘special’ member function whose task is to initialize the objects of
its class. It is special because its name is the same as the class name. The constructor is invoked whenever an
object of its associated class is created. It is called constructor because it constructs the values of data
members of the class.
class student
{
int roll_no;
float marks;
public:
student( ) // constructor
{
roll_no = 0;
marks = 0.0;
}
Types of Constructors:
Example:
#include<iostream.h>
#include<conio.h>
class code
{
int roll_no;
float marks;
public:
code( ) // default constructor
{
roll_no = 1;
marks = 2.5;
}
Example:
#include<iostream.h>
#include<conio.h>
class code
{
int roll_no;
float marks;
public:
code( int a, float b) // parameterized constructor
{
roll_no = a;
marks = b;
}
int main( )
{
code A(100, 10.5); // parameterized constructor called
(c) Copy Constructor: A copy constructor is a constructor that defines and initializes an
object with another object. It takes the form classname (classname &).
Example:
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code( int a) // parameterized constructor
{
int main( )
{
code A(100);
code B(A); // copy constructor called
Characteristics of Constructors:
Destructors: A destructor, as the name implies, is used to destroy the objects that have been created
by a constructor. Like a constructor, the destructor is also a member function whose name is the same as the
class name but is preceded by a tilde (~). It is called automatically by the complier when an object is
destroyed. A destructor cleans up the storage (memory area of the object) that is no longer accessible.
~sample ( ) // destructor
{
cout << “ Destructor at work \n”;
}
};
int main( )
{
sample s1(50, 100);
}
During construction of an object by the constructor, resources may be allocated for use. For example, a
constructor may have opened a file and a memory area may be allotted to it. Similarly, a constructor may
have allocated memory to some other objects. These allocated resources must be deallocated before the
object is destroyed. A destructor will work here for this task and performs all clean-up tasks (like closing a
file, deallocating and releasing memory area) automatically. Therefore, a destructor is equally useful as a
constructor is.
Characteristics of Destructors:
(a) They are invoked automatically when the objects are destroyed.
(b) A destructor never takes any argument nor does it return any value.
(c) They cannot be inherited.
(d) A destructor may not be static.
(e) It is not possible to take the address of a destructor.
Static Data Members: Static data members of class are those members which are shared by all the
objects. Static data member has a single piece of storage, and is not available as separate copy with each
object, like other non-static data members.
Static member variables (data members) are not initialized using constructor, because these are not
dependent on object initialization.
It is initialized to zero when the first object of its class is created. All the static members must be defined
outside the class definition.
Example:
#include <iostream.h>
#include <conio.h>
class item
{
static int count;
int num;
public:
item( )
{
num = 0;
}
void getcount( )
{
cout << “\n Number:- “ << num;
cout << “\n Count:- “ << count;
}
};
int item::count = 0;
void main( )
{
item ob1, ob2;
cout << “ \n Object Creation”;
ob1.getcount( );
ob2.getcount( );
cout << “\n Object Output”;
ob2.get_data(200);
ob1.getcount( );
ob2.getcount( );
getch( );
}
Static Member
Static Functions :- Static member functions are the functions that do not require an instance of the
Data Methods:
class, and are called the same way as the static data member.
Static member function can only access static data members, other static member functions and any other
functions from outside the class.
A static member function can be called even if no object of class exists and they are accessed using only the
class name and scope resolution operator.
Example:
#include <iostream.h>
#include <conio.h>
class item
{
static int count;
int num;
public:
void set_num( )
{
num = count + 1;
}
void show_num( )
{
cout << “\n Number:-“ << num;
}
void main( )
{
item ob1, ob2;
Inheritance: Inheritance is the capability of one class of things to inherit capabilities or properties
from another class.
The class, whose properties are inherited, is called Base Class or Super Class and the class that inherits these
properties, is called Derived Class or Sub Class.
Inheritance helps in reducing the overall code size of the program, which is an important concept in object-
oriented programming.
Single Level Inheritance: When a subclass inherits only from one base class, it is known as single
inheritance.
X Base Class
Y Sub Class
Multiple Inheritance: When a subclass inherits only from multiple base classes, it is known as
multiple inheritance.
X Y Base Classes
Z Sub Class
Syntax:
class X
{
Block of code;
----
----
};
class Y
{
Block of code;
----
----
};
class Z : public X, public Y
{
Block of code;
----
----
};
X Base Class
Y Z W Sub Classes
Syntax:
class X
{
Block of code;
----
----
};
class Y : public X
{
Block of code;
----
----
};
class Z : public X
{
Block of code;
----
};
class W : public X
{
Block of code;
----
};
Multilevel Inheritance: The transitive nature of inheritance is reflected by this form of inheritance.
When a subclass inherits from a class that itself inherits from another base class, it is known as multilevel
inheritance.
Y
Sub Class of X and Base Class of Z
Z
Syntax:
class X
{
Block of code;
----
----
};
class Y : class X
{
Block of code;
----
----
};
class Z : public Y
{
Block of code;
----
----
};
Hybrid Inheritance: Hybrid inheritance combines two or more forms of inheritance, i.e. when a sub
class inherits from multiple base classes and all of its base classes inherit from a single base class, this form
of inheritance is known as hybrid inheritance.
Y Z
W
Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 18 of 25
Syntax:
class X
{
Block of code;
----
----
};
class Y : public X
{
Block of code;
----
----
};
class Z : public X
{
Block of code;
----
};
class W : public Y, public Z
{
Block of code;
----
};
Need of Inheritance:
1. Capability to express the inheritance relationship which ensures the closeness with the real world
models.
2. Reusability: The advantage of reusability are faster development time, easier maintenance, and
easy to extend. Inheritance allows the addition of additional features to an existing class without
modifying it. One can derive a new class from an existing one and add new features to it.
3. One reason is transitive nature of inheritance. If a class B inherits properties of another class A, then
all subclasses of B will automatically inherit the properties of A. This property is called transitive
nature of inheritance.
void indata( )
{
cout<< “Enter the value of c and d:-“;
cin>>c >>d;
}
void outdata( )
{
cout<<”\n The value of a is :” << c << “and b: “ << d;
}
};
void main( )
{
derived obj;
obj.getdata( );
obj.indata( );
obj.putdata( );
obj.outdata( );
getch( );
}
cin>>c >>d;
}
void outdata( )
{
cout<<”\n The value of c is :” << c << “and d: “ << b;
}
};
class derived : public base2
{
int e, f;
public:
void input( )
{
cout<< “Enter the value of e and f:-“;
cin>>e >>f;
}
void output( )
{
cout<<”\n The value of e is :” << e << “and f: “ << f;
}
};
Multiple Inheritance:
#include<iostream.h>
#include<conio.h>
class CPolygon
{
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a;
height = b;
}
};
class COutput
{
public:
void output(int i)
{
cout<< i <<endl;
}
};
class CRectangle : public CPolygon, public COutput
{
public:
int area( )
{
int r;
r = width * height;
return(r);
}
};
Hierarchical Inheritance:
#include<iostream.h>
#include<conio.h>
class base
{
int a, b;
public:
void getdata( )
{
cout<< “Enter the value of a and b:-“;
cin>>a >>b;
}
void putdata( )
{
cout<<”\n The value of a is :” << a << “and b: “ << b;
}
};
class derived1 : public base
{
int c, d;
public:
void indata( )
{
cout<< “Enter the value of c and d:-“;
cin>>c >>d;
}
void outdata( )
{
cout<<”\n The value of c is :” << c << “and d: “ << b;
}
};
class derived2 : public base
{
int e, f;
void main( )
{
derived1 obj1;
derived2.obj2;
obj1.getdata( );
obj1.indata( );
obj2.getdata( );
obj2.input( );
obj1.putdata( );
obj1.outdata( );
obj2.putdata( );
obj2.output( );
getch( );
}
Hybrid Inheritance:
#include<iostream.h>
#include<conio.h>
class base
{
int a, b;
public:
void getdata( )
{
cout<< “Enter the value of a and b:-“;
cin>>a >>b;
}
void putdata( )
{
cout<<”\n The value of a is :” << a << “and b: “ << b;
}
};
class derived1 : public base
void main( )
{
derived1 obj1;
derived2.obj2;
obj1.getdata( );
obj1.indata( );
obj2.getdata( );
obj2.input( );
obj1.putdata( );
obj1.outdata( );
obj2.putdata( );
obj2.output( );
getch( );
}