Bca3rd Sem Unit 1 TO 4 C++
Bca3rd Sem Unit 1 TO 4 C++
BY ZIA AKBAR 1
Principals of OOPs
Unit 1
BY ZIA AKBAR 2
Procedure oriented programming
• POP Definition
• Procedural Oriented
Programming is one of the
Main program programming methods
where the main focus is on
functions or procedures
required for computation,
Function function instead of data.
Function
1 2 3 • The program is divided into
functions, and the task is
Function Function done sequentially. These
4 5 functions share the global
data or variables, and there is
an exchange of data among
Function
those functions.
6
• Languages: C, Pascal,
FORTRAN
BY ZIA AKBAR 3
characteristics
BY ZIA AKBAR 4
Object oriented programming
• Object-Oriented Programming
Object
1 • is one of the high-level programming
languages in which a program is
divided into objects.
BY ZIA AKBAR 5
characteristics
• Emphasis on data rather than functions.
BY ZIA AKBAR 6
Contd…
BY ZIA AKBAR 7
Procedural vs. Object-Oriented
Programming
• The unit in procedural programming is function, and unit in
object-oriented programming is class
BY ZIA AKBAR 8
C vs C++
• Similarities
1. Same built-in data types
BY ZIA AKBAR 9
differences
in C all code exists in 1. in C++ classes are used to
function and the model the behavior of data
paradigm is that we objects, behavior is
manipulate data with represented by functions and
functions those functions are used to
modify the object’s data
1. in C a struct is used to
make an aggregate type
and cannot have 1. in C++ struct there may be
associated functions associated (part of the
struct) functions to process
the data
BY ZIA AKBAR 10
Contd….
BY ZIA AKBAR 11
• C does not have any classes or objects. It is procedure and function
driven. There is no concept of access through objects and structures
are the only place where there is a access through.
• C structures have a different behavior compared to c++ structures.
Structures in c do not accept functions as their parts.
BY ZIA AKBAR 12
• Undeclared functions in c++ are not allowed. The function has
to have a prototype defined before the main() before use in c++
although in c the functions can be declared at the point of use.
BY ZIA AKBAR 13
• C has a top down approach whereas c++ has a bottom up
approach.
BY ZIA AKBAR 14
Basic concepts
• Objects
• Classes
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
BY ZIA AKBAR 15
objects
• Objects are the basic run time entities.
• Objects are class variables and use classes data and functions.
• Represents real world things and user defined data like bank
account, any item, any place etc...
BY ZIA AKBAR 16
Concept: An object has behaviors
BY ZIA AKBAR 17
An object has state
• An object contains both data and methods that
manipulate that data
– The data represent the state of the object
– Data can also describe the relationships between
this object and other objects
BY ZIA AKBAR 18
Example: A “Rabbit” object
BY ZIA AKBAR 19
Example: A “man” object
BY ZIA AKBAR 20
Classes
BY ZIA AKBAR 21
Concept: Classes describe objects
• Every object belongs to (is an instance of) a class.
BY ZIA AKBAR 22
Data abstraction and
encapsulation
• Data abstraction is representing a method of show only essential
features with out including back ground detail.
BY ZIA AKBAR 23
Inheritance
• Process by which object of one class acquire the
properties of another class
Engineers
Automobile Contd…
BY ZIA AKBAR 24
Contd….
BY ZIA AKBAR 25
Polymorphism
• To take more than one form.
BY ZIA AKBAR 26
Example :-
DRAW
( TRIANGLE)
BY ZIA AKBAR 27
Binding
binding
Object
Function information
name
BY ZIA AKBAR 29
Benefits of OOPs
BY ZIA AKBAR 30
Object oriented
languages
BY ZIA AKBAR 32
Applications of C++
It is suitable for following programming task
1. development of editors
2. compilers
3. databases
4. communication systems
5. any real life systems
BY ZIA AKBAR 33
General form of a C++ program
• // Program description
• #include directives
• int main()
• {
• constant declarations
• variable declarations
• executable statements
• return 0;
• }
BY ZIA AKBAR 34
C++ statements
• #include<iostraem.h>
• Int main()
• { float num 1,num2,sum,avg;
– Cout<<“enter two numbers:”;
– Cin>>num1;
– Cin>>num2;
– sum=num1+num2;
– Avg=sum/2;
– Cout<<“sum”= <<sum <<“\n”;
– Cout<<“average”= <<avg <<“\n”;
– Return 0;
}
BY ZIA AKBAR 35
Iostream
it add the contents of the iostream file to the prroogram. The
iostream contains the declarations of COUT and << operator.
In old versions of compiler iostream.h header file is used.
BY ZIA AKBAR 37
• Cout is predefined object in c++ that
corresponds to the standard output
stream(moniter).
• The <<operator is known as insertion
operator.
• It dispaly value in to moniter and assign
it to cout.
BY ZIA AKBAR 38
Structure of c++ program
BY ZIA AKBAR 39
Class name • C++ program with classes
BY ZIA AKBAR 42
The Operators new and delete
• The unary operators new and delete are available to
manipulate free store.
• They are more convenient than the C functions
malloc(), calloc(), and free().
• Free store is a system-provided memory pool for
objects whose lifetime is directly managed by the
programmer.
• This adds responsibility to the programmer and can
easily lead to problems such as memory leaks.
• On the other hand, manipulating free store is an
efficient and flexible way to handle data structures
such as trees and lists.
BY ZIA AKBAR 43
The Operators new and delete
• The programmer creates an object using new, and destroys
the object using delete.
• The operator new is typically used in the following forms:
– new type-name
– new type-name initializer
– new type-name [expression]
• In each case, there are at least two effects:
– An appropriate amount of store is allocated from
free store to contain the named type.
– The base address of the object is returned as
the value of the new expression.
BY ZIA AKBAR 44
The Operators new and delete
• Example:
• …
• int *p, *q;
• p = new int(5);
• q = new int[10];
• …
• In this code,
• the pointer variable p is assigned the address of the store
obtained,
• The location pointed at by p is initialized to the value 5,
• the pointer variable q is assigned the base address of an int
array of size 10.
BY ZIA AKBAR 45
The Operators new and delete
• Notice the following things:
• When memory is unavailable, the operator new can
either throw a bad_alloc exception or return the value
0.
• If no initializer is provided, the content of the
allocated memory is undefined.
• Arrays cannot be initialized using the new operator.
• Objects created by the new operator always need to
be destroyed by the delete operator as soon as they
are not used by the program any more.
BY ZIA AKBAR 46
The Operators new and delete
• The operator delete destroys an object created by new.
• This returns its allocated storage to free store for reuse.
• The operator delete is used in the following forms:
– delete expression
– delete [] expression
• The first form is used when the corresponding new expression
has not allocated an array.
• The second form (empty brackets) is used when the original
allocation was an array.
• The return type of delete is void (no return value).
BY ZIA AKBAR 47
The Operators new and delete
• Example: Dynamic allocation of an array
• int main() Starting the program:
• {
• int *data; Enter array size: 4
• int size; 0
• cout << “\nEnter array size: “; 1
2
• cin >> size; 3
• data = new int[size];
• for (int j = 0; j < size; j++)
• cout << (data[j] = j) << ‘\n’;
• delete [] data;
• return 0;
• }
BY ZIA AKBAR 48
BY ZIA AKBAR 49
CLASS & OBJECT
UNIT 2
BY ZIA AKBAR 50
• Class: A Class is a user defined data type
to implement an abstract object. Abstract
means to hide the details. A Class is a
combination of data and functions.
BY ZIA AKBAR 51
• A class definition begins with the
keyword class.
• The body of the class is contained
within a set of braces, { } ; (notice the
semi-colon).
class class_name Any valid
{ identifier
….
….
Class body (data member +
….
methods)
methods
};
BY ZIA AKBAR 52
• Within the body, the keywords private:
and public: specify the access level of
the members of the class.
– the default is private.
BY ZIA AKBAR 54
– private:
• Accessible only to member functions of class
• Private members and methods are for internal
use only.
PUBLIC
BY ZIA AKBAR 56
class class_name
{
private: private members or
… methods
…
…
public:
… Public members or methods
…
…
};
BY ZIA AKBAR 57
• This class example shows how we can
encapsulate (gather) a circle
information into one package
No need(unit
for othersor
classes to
class)
class Circle access and retrieve its value directly.
The
{
class methods are responsible for
private: that only.
double radius;
public:
void setRadius(double r); double They are accessible from outside
getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
BY ZIA AKBAR 58
Class Example (Problem)
#include<iostream.h> void main()
#include<stdio.h> {
class student student s;
{ cout<<“enter the rollno.:”;
int rollno; cin>>s.rollno;
char name[20]; cout<<“enter the name:”;
}; gets(s.name);
cout<<“rollno:”<<s.rollno;
cout<<“\nname:”;
puts(s.name);
}
BY ZIA AKBAR 59
Class Example (Solution)
#include<iostream.h> void main()
#include<stdio.h> {
class student student s;
{ cout<<“enter the rollno.:”;
public: cin>>s.rollno;
int rollno; cout<<“enter the name:”;
char name[20]; gets(s.name);
}; cout<<“rollno:”<<s.rollno;
cout<<“\nname:”;
puts(s.name);
}
BY ZIA AKBAR 60
Implementing class
methods
• There are two ways:
1. Member functions defined outside class
• Using Binary scope resolution operator (::)
• “Ties” member name to class name
• Uniquely identify functions of particular class
• Different classes can have member functions with
same name
– Format for defining member functions
ReturnType ClassName::MemberFunctionName( ){
…
}
BY ZIA AKBAR 61
Member Function
Defining Inside the Class
#include<iostream.h>
#include<stdio.h>
class student
{ Data Members (Private : in this example)
int rollno;
char name[20];
public:
void getdata()
{
cout<<“enter the rollno.:”; Member Functions (Public: in this example)
cin>>rollno;
cout<<“enter the name:”;
gets(name);
}
void putdata()
{
cout<<“rollno:”<<rollno;
cout<<“\nname:”;
puts(name);
}
}; Calling member function
void main()
{
student s;
s.getdata();
s.putdata();
}
BY ZIA AKBAR 62
#include<iostream.h>Member Function
#include<stdio.h>Defining Outside the Class
class student void student: :: putdata()
{ {
int rollno; cout<<“rollno:”<<rollno;
char name[20]; cout<<“\nname:”;
public: puts(name);
void getdata(); }
void putdata(); void main()
}; {
void student :: getdata() student s,s1;
{ S1=s;
cout<<“enter the rollno.:”;s.getdata();
cin>>rollno; s.putdata();
cout<<“enter the name:”;}
gets(name); BY ZIA AKBAR 63
}
Characteristics of member
function
• Different classes have same function name. the
“membership label” will resolve their scope.
• Member functions can access the private data of
the class .a non member function cannot do this.
(friend function can do this.)
• A member function can call another member
function directly, without using the dot operator.
BY ZIA AKBAR 64
Accessing Class Members
• Operators to access class members
– Identical to those for structs
– Dot member selection operator (.)
• Object
• Reference to object
– Arrow member selection operator (->)
• Pointers
BY ZIA AKBAR 65
Creating an object of a
Class
• Declaring a variable of a class type creates an
object. You can have many variables of the
same type (class).
– Instantiation
• Once an object of a certain class is
instantiated, a new memory location is
created for it to store its data members and
code
• You can instantiate many objects from a class
type.
– Ex) Circle c; Circle *c;
BY ZIA AKBAR 66
Memory Allocation of
Object
class student
{ rollno – 2 bytes
int rollno;
char name[20]; name- 20 bytes
int marks;
};
marks- 2 bytes
student s;
24 bytes s
BY ZIA AKBAR 67
Array of objects
• An array can be of data type including struct an
class .
• The array of class type variable is known as array
of object.
• We can declare array of object as following way:-
Class _name object [length];
Employee manager[3];
1. We can use this array when calling a member function
2. Manager[i].put data();
3. The array of object is stored in memory as a multi-
dimensional array. BY ZIA AKBAR 68
Object as function
arguments
• This can be done in two ways:-
BY ZIA AKBAR 69
( pass by value)
• A copy of the object is passed to the function,
any changes made to the object inside the
function do not affect the object used to call
function.
(pass by reference)
BY ZIA AKBAR 72
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z; 5 7
cout<<“enter imaginary part:”; X.getdata( ); 6 8
cin>>imag;
Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0)
cout<<real<<“+”<<imag<<“i”; }
else
cout<<real<<imag<<“i”;
}
BY ZIA AKBAR 73
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z; 5 7
cout<<“enter imaginary part:”; X.getdata( ); 6 8
cin>>imag;
Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0) 5 7
cout<<real<<“+”<<imag<<“i”; }
6 8
else
cout<<real<<imag<<“i”;
} A B
BY ZIA AKBAR 74
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum(Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z; 5 7 12
cout<<“enter imaginary part:”; X.getdata( ); 6 8 14
cin>>imag;
Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0) 5 + 7 =
cout<<real<<“+”<<imag<<“i”; }
6 + 8 =
else
cout<<real<<imag<<“i”;
} A B
BY ZIA AKBAR 75
Passing Object
#include<iostream.h> void complex : : sum ( Complex A, Complex B)
class Complex {
{ real = A.real + B.real;
float real, imag; imag= A.imag + B.imag;
public: }
void getdata( );
void putdata( ); void main( )
void sum (Complex A, Complex B); {
}; Complex X,Y,Z;
void Complex : : getdata( ) X.getdata( );
{ Y.getdata( );
cout<<“enter real part:”;
cin>>real;
Z.sum(X,Y); 5 7 12
Z.putdata( );
cout<<“enter imaginary part:”; 6 8 14
}
cin>>imag;
} X Y Z
void Complex : : putdata( )
{
if (imag>=0) 12 + 14 i 5 + 7 =
cout<<real<<“+”<<imag<<“i”; 6 8
else + =
cout<<real<<imag<<“i”;
} A B
BY ZIA AKBAR 76
#include<iostream.h>
class Complex
{
float real, imag; Returning Object
Complex Complex : : sum (Complex
public:
B)
void getdata( );
{
void putdata( );
Complex temp;
Complex sum (Complex B);
temp.real=real + B.real;
};
temp.imag= imag + B.imag;
void Complex : : getdata( )
return temp;
{
}
cout<<“enter real part:”;
void main ( )
cin>>real;
{
cout<<“enter imaginary part:”;
Complex X, Y, Z;
cin>>imag;
X.Getdata( );
}
Y. getdata( );
void Complex : : putdata( )
Z= X.sum (Y);
{
Z.putdata( );
if (imag>=0)
}
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”; BY ZIA AKBAR 77
}
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( )
{
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
BY ZIA AKBAR 78
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
BY ZIA AKBAR 79
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7
8
BY ZIA AKBAR 80
B
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7
8
BY ZIA AKBAR 81
B
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7 12
8 14
BY ZIA AKBAR 82
B temp
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7 12
8 14
BY ZIA AKBAR 83
B temp
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7 12
8 14
BY ZIA AKBAR 84
B temp
#include<iostream.h>
Returning Object
Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
12 + 14 i 7 12
8 14
BY ZIA AKBAR 85
B temp
Friend function
• C++ allows a way through which a function can
access the private data of a class.
BY ZIA AKBAR 88
Characteristics
• It is not in the scope of the class in which it has
been declared as friend.
• it is not in the scope of class so it cannot be
called using object of that class.
• It can be invoked like normal function ,without
object.
BY ZIA AKBAR 89
• It can be declared either in public or private part
with out affecting its meaning.
Friend Function
float real, imag; Complex sum (Complex A, Complex B)
public: {
void getdata( ); Complex temp;
Friend void putdata( complex ); temp.real=A.real + B.real;
friend Complex sum (Complex A, Complex B); temp.imag= A.imag + B.imag;
}; return temp;
void Complex : : getdata( ) }
{ void main ( )
cout<<“enter real part:”; {
cin>>real; Complex X, Y, Z;
cout<<“enter imaginary part:”; X.getdata( );
cin>>imag; Y. getdata( );
} Z= sum (X,Y);
void putdata(complex s ) putdata(Z );
{ }
if (s.imag>=0)
cout<<s.real<<“+”<<s.imag<<“i”;
else
cout<<s.real<<s.imag<<“i”;
}
BY ZIA AKBAR 91
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( )
{
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
BY ZIA AKBAR 92
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
BY ZIA AKBAR 93
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 7
6 8
BY ZIA AKBAR 94
A B
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 =
6 + 8 =
BY ZIA AKBAR 95
A B temp
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 = 12
6 + 8 = 14
BY ZIA AKBAR 96
A B temp
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 = 12
6 + 8 = 14
BY ZIA AKBAR 97
A B temp
#include<iostream.h>
Friend Function Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”; 12 + 14 i
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 = 12
6 + 8 = 14
BY ZIA AKBAR 98
A B temp
• We can also declare all the member functions of
one class as the friend functions of another
class. In this case the first class is known as
FRIEND class.
and
Destructors
• A constructor has:
(i) the same name as the class itself
(ii) no return type ,not even void.
rc.posn(100, 100);
rc.draw();
rc.move(50, 50);
rc.draw();
}
• Expl:- ~assign()
• {
– Delete p;
}
Inheritance
BY ZIA AKBAR 152
Inheritance
• The mechanism of deriving new class from an old class is
called “INHERITANCE”.
A B
Single inheritance C
inheritance
A
D
B C
Hierarchical
C inheritance
B C
CLASS D2:PRIVATE B
CLASS D1:PUBLIC B
private private
protected protected
public public
private
protected
public
19 BY ZIA AKBAR
9
Void area(int) Void area(int,int);
Void area(int,int,int); Int main()
{
Int side=10,le=5,br=6,a=4,b=5,
c=6; Area(side);
Area(le,br);
Area(a,b,c); Getch(); Return 0;
}
Void area(int x)
{ cout<<“area is”<<x*x;
}
Void area(int x,int y)
{cout<<“area of rectang;e”
=<<x*y;
}
Void area(int x,int y,int z)
{cout<<“volume is”<<x*y*z;
}
20 BY ZIA AKBAR
0
Involves
following
Steps.
Compiler first tries to find the Exact match in
which the type of argument are the same,and
uses that func.
If an exact match is not found,the compiler user
the integral promotions to the actual argument
such as,char to int, float to double.
When either of them fails ,build in conversions are
used(implicit conversion) to the actual arguments
and then uses the function whose match is
unique.but if there are multiple matches,then
compiler will generate an error message.
20 BY ZIA AKBAR
1
long
For ex: square(long n)
• long square(double x)
• Now a func. call such as square(10) will
cause an error because int argument can
be converted into long also and double
also.so it will show ambiguity.
4. Conditional; operator{? :}
BY ZIA AKBAR 210
Unary operator overloading
#include<iostream.h> Void unary:: operator –( )
Class unary {
{
int x,y,z; x=-x;
public: y=-y;
void getdata(int a,int b,int c); z=-z;
void display(); }
void operator –();
}; int main()
Void unary:: getdata(int a, int b,int c) {
{ unary u;
x=a; u.getdata(10,-20,30) ;
y=b;
z=c; cout<<“u=“;
} u.display();
Void display() -u;
{ cout<<“u=“;
cout<<x<<“”;
cout<<y<<“ “; u.display( );
cout<<z<< “ “; return 0;
} }
• Synatx:-
Class class_name{
{
Virtual return type function_name(signature)
{
…….
…….
}
}; BY ZIA AKBAR 218
• We should use pointer object of base class
for accessing the virtual function , we can’t
use the object name and dot operator.
Template
• Function template
– Function templates are special functions that
can operate with generic types. This allows us
to create a function template whose
functionality can be adapted to more than one
type or class without repeating the entire code
for each type.
BY ZIA AKBAR 234
• In C++ this can be achieved using template
parameters. A template parameter is a special kind
of parameter that can be used to pass a type as
argument: just like regular function parameters can
be used to pass values to a function.
function_name (parameters);
BY ZIA AKBAR 236
function template
#include <iostream>
template <class T>
T GetMax (T a, T b)
{
T result;
result = (a>b)? a : b;
return (result);
}
int main ()
{
Int i=5, j=6, k;
long l=10, m=5, n;
k=GetMax(i,j);
n=GetMax(l,m);
cout << k << endl;
cout << n << endl;
return 0;
}
int i;
long l;
k = GetMax (i,l);
This would not be correct, since our GetMax function template
expects two arguments of the same type, and in this call to it we
use objects of two different types.
We can also define function templates that accept more than one
type parameter, simply by specifying more template parameters
between the angle brackets. this is known as template function
with multiple parameter. For example:
int i,j;
long l;
i = GetMin<int,long> (j,l);
• For example:
template <class T>
class mypair
{
T values [2];
public:
mypair (T first, T second)
{
values[0]=first;
values[1]=second;
}
};
#include <iostream>
template <class T>
class mypair
{
T a, b;
public:
mypair (T first, T second)
{
a=first;
b=second;
}
T getmax ();
};
template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}
int main ()
{
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
Exception object
catch block
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
BY ZIA AKBAR 253
throw(a-b);
EXCEPTION HANDLING MECHANISM (CONT…)
Often, Exceptions are thrown by functions that are
invoked from within the try blocks.
Invoke function
Throw exception
catch (…)
{
// statement for processing
// all exception
}
namespace identifier
{
entities
}
Where identifier is any valid identifier and entities is the set of classes,
objects and functions that are included within the namespace. For
example:
namespace myNamespace
{
int a, b;
}
In this case, the variables a and b are normal variables declared within a
namespace called myNamespace. BY ZIA AKBAR 275
BY ZIA AKBAR 276
Accessing a namespace
Member
• The scope of a namespace member is
local to the namespace
• Usually two ways a namespace
member can be accessed outside the
namespace
• One way is to use the syntax:
namespace_name::identifier
• To access the member rate of the
namespace globalType, the following
BY ZIA AKBAR 277
statement is required:
Accessing a namespace
Member (continued)
• To access the function printResult, the
following statement is required:
globalType::printResult();
– Alternatively, the using declaration allows the names
of the elements to be used directly
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
BY ZIA AKBAR 282
Defining class inside the
namespace
Namespace MYNAME The defining object of the
{ class best.
Class best Syntax:-
{ Namespace name:: class
String name; name object name;
Public:
Ex:
Best( cost char* temp)
{ MYNAME ::best obj(“hello”)
Name= temp;
}
};
}
BY ZIA AKBAR 283
Declaring inside and
defining of function
outside
Namespace myspace
namespace:
{ function declaration inside namespace
Int num;
Void input();
}
Void myspace::input()
{
code; function definition outside namespace
}
Myspace::input(); function call
When we using the USING method then function call should be
as follows:
Using namespace myspace;
Input();
Namespace
alias_name=namespace_name;
return 0;
}
– Ifstream in(“data”)
EXAMPLE
#include<iostream.h>
#include<fstream.h>
Int main
{Char name[30];
Ofstream outf(“file.txt”);
Cout<<“Enter employee name”;
Cin>> name;
Outf<<name;
Outf.close();
Ifstream inf(“file.txt”);
Inf>>name
Inf.close();
Return 0;
}
OUTPUT: OPEN A FILE “FILE.TXT” THEN DISPLAY YOUR NAME
INTO THAT FILE AND READ YOUR NAME FROM FILE”FILE .TXT”.