C++first Unit
C++first Unit
C++ - 41 Marks
Data types in C++ - 3M
Data Types
class Function
Integral structure Array
void Floating Point
Union Pointer
Enumeration
For example:
void fun( int x, int y); //function prototype
void main( )
{
----
fun(x,y); //call by value(function call)
----
}
void fun( int x, int y) // function definition
{
-----
}
TRIAL MODE − Click here for more information
Call by reference
• When a function is called by a program the address of the
actual arguments are copied on to the formal arguments
i.e the formal and actual arguments are referring to same
memory location.
• Therefore change in value of formal argument affects the
value of actual arguments.
• The content of a variable that are altered within the
function are returned to calling portion of a program in
the altered form.
TRIAL MODE − Click here for more information
For example:
void fun( int *x, int *y); //function prototype
void main( )
{
----
fun( x, y); //call by reference(function call)
----
}
void fun( int *x, int *y) // function definition
{
-----
}
TRIAL MODE − Click here for more information
class classname(object)
{
access_specifier:
data and functions
access_specifier:
data and functions
access_specifier:
data and functions
};
class is a keyword, access specifier are public, private, protected which
decides scope and visibility of data members and member functions
declared in the class definition.
TRIAL MODE − Click here for more information
e.g.
class try
{
public:
void display();//member function
};
//member function definition outside class
void try::display()
{
cout<<“\n C++Programming”;
}
TRIAL MODE − Click here for more information
void circle::area()
{
cout<<"Enter r";
cin>>r;
a=3.142*r*r;
cout<<"\n The area of circle="<<a;
}
TRIAL MODE − Click here for more information
classname(parameter list)
{
//body of parameterised constructor
}
TRIAL MODE − Click here for more information
3)Copy constructor
It is a constructor with only one parameter of its same
type that assigns to every nonstatic class member
variable of the object a copy of the passed object. It is
always used when the complier has to create a
temporary object of a class object.
Syntax: classname(classname var)
{
//body of copy constructor
}
TRIAL MODE − Click here for more information
Ex.
#include<iostream.h>
#include<conio.h>
class sample
{
int x;
public:
sample(int a)//parameterised constructor
{
x=a;
}
TRIAL MODE − Click here for more information
sample(sample i)
{
x=i.x;
}
void display()
{
cout<<"x="<<x<<"\t";
}
};
TRIAL MODE − Click here for more information
void main()
{
clrscr();
sample obj(5);
sample obj1(obj); //copy constructor invoked
obj.display();
obj1.display();
getch();
}
Output: x=5 x=5
TRIAL MODE − Click here for more information
Destructor
A mechanism which automatically destroys an object
when it gets invalid. Destructors are used to release any
resources allocated by the object’s constructor. They are
automatically for you and there’s only one destructor for
each object. The name of the destructor is the same as
class name but preceded by tilde(~). They take no
arguments.
Ex.
#include<iostream.h>
#include<conio.h>
TRIAL MODE − Click here for more information
class ratio
{
public:
ratio() //constructor
{
cout<<“\n The object is born”;
}
~ratio() //destructor
{
cout<<“\n The object dies”;
}
TRIAL MODE − Click here for more information
Base class
Derived class
2)Multiple Inheritance-
When a class is derived from several base classes it is called as
multiple Inheritance.
Base 1 Base 2 Base n
Derived class
TRIAL MODE − Click here for more information
3)Multilevel Inheritance
The mechanism of deriving one class from another derived
class is called multilevel Inheritance.
Base class
Derived 1
Derived n
4)Hierarchical Inheritance
When several derived classes are derived from a single base
class then it is called as Hierarchical Inheritance.
TRIAL MODE − Click here for more information
Base class
5) Hybrid Inheritance-
The inheritance which involves more than one inheritances is called as hybrid Inheritance.
Base
Derived 1 Derived 2
Derived