You are on page 1of 28

C++

.:
fhdalqasem@yahoo.com

:
(Introduction To Object Oriented Programming In C++)
BPB PUBLICATION Yashavant Kanetkar
OBJECT ORIENTED PRORAMMING IN C++ 2

- -

):(Class & Object

class (C++) ++ ) (C
Data ) (Functions ) (object
.

(C++) ++ ) (structures ) (classes


) (Functions (C++) ++ ) (C
((C++))++
) (C .

)(Declaration of Class

) (Member Scope
:

class class_name

private:
;DataMembers declaration
;FunctionMembers declaration

public:
;DataMembers declaration
;FunctionMembers declaration

;}

) (class ) (Compiler
.

) :(class members
) (Data ) (Functions
) (method ) (Data Members
).(Member functions

)(private) (public
) (Encapsulation ) (data hiding
) ( ) (private
) (public
OBJECT ORIENTED PRORAMMING IN C++ 3

) (scope
) (Main ) (private .

class rectangle
{ private:
;int len,br
public:
;)(void getdata
;)void setdata(int I,int b
;) (void displaydata
;) (void area_peri
;}

) (RECTANGLE
)(br) (len
.
/ ) (class body
) (public )(
) (private
.

/
.

/ ):(class instance
) (float y
:
;Float y
y float
) ( ) (instance
) ( ) (rectangle :
;rectangle r1,r2
) r1 (r2 ) (rectangle
) (rectangle
c++
.
)
)( (sizeof
object r1
r2
.
OBJECT ORIENTED PRORAMMING IN C++ 4

class
) (instance object .

):(Accessing class members


private
)( main
) ( .
dot ))((object.function
:r1
;)r1.setdata(10,20); r2.setdata(4,5
br len r1
r2 :
r1 r2
Len br Len br
10 20 4 5

) ( :


:
Class rectangle
{
private:
;int len,br
public:
)(void getdata
{
;"cout<<endl<<"enter length and breadth
;cin>>len>>br
}
)void setdata(int I,int b
{
;len=I
;br=b
}
)(void displaydata
{
;cout<<endl<<"length="<<len
;cout<<endl<<"breadth="<<br
}
)(void area_peri
OBJECT ORIENTED PRORAMMING IN C++ 5

{
;Int a,p
;a=len*br
;)p=2*(len+br
;cout<<endl<<"area="<<a
;cout<<endl<<"perimeter="<<p
}
;}
::
:
)Return-type calss_name::function_name(argument lis
{
..function body..
}
Return-type ) void int char (...
calss_name
::
function_name .
argument lis ) (.

:
.
:
. class declaration
.
. ) (inline
inline inline
:
}{ )Inline Return-type calss_name::function_name(argument lis
inline
.
)) (headfile(*.h )(
) (*.cpp libraries .
. ) (local class ) (mian function
.

:
functions
) (return .

) (scope of the member.
OBJECT ORIENTED PRORAMMING IN C++ 6

..
:
) by
(value ) (by reference
) (string.h ) (:
>#include <iostream.h
>#include <string.h

Class str
{
private:
;]char s[50
public:
)void set (char *ss
{
;)strcpy(s,ss
}
)(void print
{
;cout<<s<<endl
}
)void concat (str s2
{
;)strcat(s,s2.s
}
;}
)(void main
{
;str s1,s2
;)"s1.set("hand in
;)"s2.set("hand
;)s1.concat(s2
;)(s1.print
}
str ) ( array of characters
)( set )( )(print
)( concat )
( ) (.
OBJECT ORIENTED PRORAMMING IN C++ 7

)( main s1 )( set
s2 s1
s1 .
).(hand in hand
.. s2 s1 :
;)s2.concat(s1
;)(s2.print
: s2 )( main
) void concat (str s2
) (
str s2
.str

:

) (integers or floats
:
class sample
{
private:
;int i
public:
)void set(int ii
{
;i=ii
}
)(void print
{
;cout<<endl<<i<<endl
}
;}
void show(sample *p)//non-memeber function..
{
)for (int j=0;j<5;j++
;)(p[j].print
}
)(void main
{
;]sample s[5
;int x
OBJECT ORIENTED PRORAMMING IN C++ 8

for(int j=0;j<5;j++)
{
cin>>x;
s[j].set(x);
}

show(s);
}

++++++++++++++++++++++
OBJECT ORIENTED PRORAMMING IN C++ 9

- -

)(Constructors & Destructors

:

:
;int x
x compiler
MIN_INT
.
initialization initial values
:
;int x
;x=5
:
;int x=5
:
;)int x (5
.

:
>#include <iostream.h
class stack
{
private:
;int i
;]int a[10
public:
)(void init
{
;i=0
}
)void push (int d
{
;a[i]=d
;i++
}
)(void print
{
)for (int j=0;j<I;j++
;]cout<<endl<<a[j
}
;}
)(void main
{
OBJECT ORIENTED PRORAMMING IN C++ 10

;stack s
;)(s.init
;)s.push(10
;)s.push(20

;)(s.print
}
global stack class
) (
stack :
: push / a.
: init i .initialization
: print ) (stack .

) (data members

) (C++ ) (intialization
:
;stack s
;)(s.init
;)( init
.. ) ( ..
:
;stack s
..
)(.init

:Declaring and defining constructors


) (member function :
)(Stack
{
;I=0
}
)( init )( stack

.
:
;stack s
constructor
:
;)Int x=3; //the same statement like int x (3
:
;Int x
;x=0
OBJECT ORIENTED PRORAMMING IN C++ 11

)
( ..
encapsulation :
. .
. .

)( .. .
)(::
:
)( stack::stack
{
;I=0
}

: characteristics of constructer
(a
.
(b )( .
(c ) (
.
(d void .
(e ) ... public(.
(f .. ..
) (zero-arguments ) ( parameterized-
. arguments
(g ) (default value
.
(h .. ).(overloading

:order of constructor invocation



) (gloabal scope
:
>#include <iostream.h
class sample
{
private:
;Int i
public:
)sample(int ii
{
;I=ii
;Cout<<endl<<"constructed"<<I
}

zero-arguments .
OBJECT ORIENTED PRORAMMING IN C++ 12

;}
;)sample s1 (1
;)sample s2(2
)(void main
{
;)sample s3 (3
;)sample s4(4

;}

) destructed (
) (.

: types of constructors
( ) : (default constructor
.
) (
.. ..
:
)( Function
}{

) (zero-argumetns
) (parameterized-arguments
) (overloading .
( : parameterized
) (

) ( ..
.
..
:
"#include "iostream.h
class sample
{
private :
;int i
;float f
public:
)(sample
{
;i=0
;f=0.0
}

overloading
) ( ploymorphisim .
OBJECT ORIENTED PRORAMMING IN C++ 13

)sample(int ii,float ff
{
;i=ii
;f=ff
}
)( void print
{
;cout<<endl<<i<<endl<<f
}
;}
)(void main
{
;sample s1
)sample s2 (10, 16.78
}
s1,s2 ..
) (overloaded constructor
) (
.
s1 )( sample s2
).sample(int,float
) (overloaded constructor
.. .
:
.
); (sample s1 ) ( .
overloaded
constructor zero-arguments ..
.
. );) (sample s1 (2 ); (sample s1=2
/ .. .
( : copy constructor
)
( .
:
;callstype obj1
..
.
;classtype obj2=obj1
obj1 .obj2
); (classtype obj2=obj1
calsstype
obj2 .

) (copy constructor C++
.
OBJECT ORIENTED PRORAMMING IN C++ 14

) ..
( ) (passing by value
.. ..
.
.. (Return Value) .. .
:
: ) (
.
:
).. (passing by reference

.. .. ..
.. .
( ):(dynamic constructors
.. ) (pointer
.. C++
.
)(
.
) (new C ..
delete:
x = new int;//
delete x;//

..
.
"#include "iostream
;using namespace std

class array
{
private:
;int *a
;int dim,i
public:
array() //zero-argument constructor
{
;]a=new int[10
dim=10;//the dimension of the array
i=0;//determine the rst element of the array
}
)array(int l
{
;]a=new int[l
dim=l; //the dimension of the array
;i=0
}
OBJECT ORIENTED PRORAMMING IN C++ 15

void add(int d) //function member to fill the array


{
if (i>=dim)
{
cout<<endl<<"array boud exceed";
i=0;
return;
}
a[i]=d;
i++;
}
void print()
{
for (int j=0;j<dim;j++)
cout<<endl<<a[j];
}
};

void main()
{
array a1,a2(5);

a1.add(100);
a1.add(300);
a1.print();

a2.add(1);
a2.add(2);
a2.add(3);
a2.add(4);
a2.add(5);
a2.print();
}

:Declaring and defining destructors



.. ...!
)~(
. ~student() student() student
:
#include "iostream"
using namespace std;

class example
{
public:
example()//constractor...
OBJECT ORIENTED PRORAMMING IN C++ 16

{
;cout<<endl<<"*******inside the constructor******"<<endl
}
~example()//destructor..
{
;cout<<endl<<"~~~~~~~inside the destructor~~~~~~~~"<<endl
}
;}
)(void main
{
;example e
;cout<<endl<<"at first we get the constructor message"<<endl
;cout<<endl<<"then before the program is ended we get the other message"<<endl
}
))( (~example .
).(void
:
)(example::~example
{
}
:
;)(~example
: characteristics of destructors
(a scope

.
(b ) ( .
(c .. overloading
.. .
(d .. )( .

++++++++++++++++++++++

--

Operator Overloading


C++
//!.
OBJECT ORIENTED PRORAMMING IN C++ 17

operator key word


: + * .. %
.

.C++

)( ..
str1,str2
" "ahmad " "basem str1+str2 :
;"char str1[20]="ahmad
;"char str2[]="basem
;]char str[20
;)strcpy(str3,str1
;)strcat(str3,str2
str1 str3 str2 str3 str3
)" ("ahmadbasem :
;str3=str1+str2
.

..
.. .
) (polymorphism
.

:overloading unary operations


..
C++ :
;X++
) . (--
++ : index
"#include "iostream
;using namespace std
class index
{
private:
;int count
public:
)(index
{
;count=0
}
)(void operator++
{
;++count
}
)(void showdata
{
OBJECT ORIENTED PRORAMMING IN C++ 18

;cout<<count
}
;}

)(void main
{
;index c

;"=cout<<endl<<"c
;)(c.showdata

;++c
;"=cout<<endl<<"c
;)(c.showdata

;++c
;"=cout<<endl<<"c
;)(c.showdata
}

++ index
operator .
:
)(void operator++
operator ++
.
count )( index
++ c .. .
:
;c=0
;c=1
;c=2
++c :
;)(c.operator++
..
++c ++count .. index
)( c.operator++ ) int float double(
.
:
;d=++c
index void
.. compiler :
error C2679: binary '=' : no operator found which takes a right-hand operand of type
''void
d
.indexs
OBJECT ORIENTED PRORAMMING IN C++ 19

( void operator++())
:
#include "iostream"
using namespace std;
class index
{
private:
int count;
public:
index()
{
count=0;
}

index operator++()
{
++count;
index temp;
temp.count=count;
return temp;
}
void showdata()
{
cout<<count;
}
};

void main()
{
index c,d;

cout<<endl<<"c=";
c.showdata();

++c;
cout<<endl<<"c=";
c.showdata();

d=++c;
cout<<endl<<"c=";
c.showdata();
cout<<endl<<"d=";
d.showdata();
}
OBJECT ORIENTED PRORAMMING IN C++ 20


) c ( temp temp
d ;.d=++c
:
;c=0
;c=1
;c=2
;d=2
))( (index operator++ :
)(index operator++
{
;++count
;)Return index (count
}
.. count :
)index (int i
{
;count=I
}
:
"#include "iostream
;using namespace std
class index
{
private:
;int count
public:
)(index
{
;count=0
}
)index (int i
{
;count=i
}
)(index operator++
{
;++count
;)return index (count
}

)(void showdata
{
;cout<<count
}
;}
OBJECT ORIENTED PRORAMMING IN C++ 21

)(void main
{
;index c,d

;"=cout<<endl<<"c
;)(c.showdata

;++c
;"=cout<<endl<<"c
;)(c.showdata

;d=++c
;"=cout<<endl<<"c
;)(c.showdata
;"=cout<<endl<<"d
;)(d.showdata

}
:
;++count
;)return index (count
:
;)return index (++count
c++;
++c ) (c++
:
)index operator++(int
{
;)return index (count++
}
) (++c) (c++
.
): ) (d=++c)((d=c++
: d=c++ :
..
.
:
.
.
. ) index operator++(int ..
..
int ..
.

: overloading binary operators


OBJECT ORIENTED PRORAMMING IN C++ 22

.. .. +
operator .. ..
.
: )(complex numbers
.. ..
.
:
)c=x+iy  c=(x,y
x y
.. ..
.
c1 c2 :
; c1 = x1 + i y1
; c2 = x2 + i y2
:
) c1 + c2 = ( x1 + x2 ) + i ( y1 + y2
) c1 * c2 = ( x1 * x2 - y1 * y2 ) + i ( x1 * y2 - x2 * y1
* +
:
"#include "iostream
;using namespace std
class complex
{
private:
;float real,image
public:
)(complex
}{
)complex (float r,float i
{
;real=r
;image=i
}
)void setdata(float r,float i
{
;real=r
;image=i
}
)complex operator+(complex c
{
;complex t
;t.real=real+c.real
;t.image=image+c.image
;return t
}
)complex operator*(complex c
{
OBJECT ORIENTED PRORAMMING IN C++ 23

;complex t
;t.real=real*c.real-image*c.image
;t.image=real*c.image+c.real*image
;return t
}
)void display(char *a
{
;cout<<a
;cout<<endl<<"real="<<real<<endl
;cout<<"imaginary="<<image<<endl
}
;}
)(void main
{
;complex c1,c2(2.5,3.9),c3
;)c1.setdata(5,9.3

;)"c1.display("c1
;)"c2.display("c2

;c3=c1+c2
;)"c3.display("c3

;c1=c2*c3
;)"c1.display("c1
;)"c2.display("c2

;c3=c2+c1*c3
;)"c3.display("c3
}
complex
.. c2 )( setdata
.

)( display ..
.
..
+ * .

) (add_complex ) (mul_complex :
;s3=s1+s2
:
))c3=(c1. add_complex(c2
; s3=s1+s2*s3!!
: +
)complex operator+(complex c
{
OBJECT ORIENTED PRORAMMING IN C++ 24

;complex t
;t.real=real+c.real
;t.image=image+c.image
;return t
}
) + ( c2
:
;s3=s1+s2
:
;)s3= s1.operator+(s2
real s1 c.real .s2

): the this pointer (this



.. ..
.. .
this ..
:
"#include "iostream
;using namespace std
class example
{
private:
;int i
public:
)void setdata(int ii
{
;i=ii;// a way to set the data
}
)(void showdata
{
;cout<<i
}
;}
)(void main
{
;example e
;)e.setdata(10
;)(e.showdata
}
.. .. .
.. this:
"#include "iostream
;using namespace std
class example
{
OBJECT ORIENTED PRORAMMING IN C++ 25

private:
;int i
public:
)void setdata(int ii
{
;cout<<"my objects address is "<<this<<endl
;this->i=ii
}
)(void showdata
{
;cout<<"my object address is "<<this<<endl
;cout<<this->i<<endl
}
;}
)(void main
{
;example e
;)e.setdata(10
;)(e.showdata
}
this->i i .. obj.i
.. this e .. .
this .

:overloading comparison operators


..
.. > operator .. complex
:
z1,z2 z1 z2 :
;z1 = x1 + i y1 ; z2 = x2 + i y2
:
;x1>x2 and y1>y2  z1>z2
:
"#include "iostream
;using namespace std
class complex
{
private:
;float real,image
public:
)(complex
}{
)complex (float r,float i
{
;real=r
;image=i
OBJECT ORIENTED PRORAMMING IN C++ 26

)int operator>(complex c
{
) if (real>c.real && image>c.image
;return 1
else
;return 0
}
;}
)(void main
{
;)complex c1(3,4),c2(2.5,3.9
)if (c1>c2
;cout<<"OK"<<endl
else
;"cout<<"conflict

}
> < ..
..
: )=> (<,<=,

++++++++++++++++++++++
OBJECT ORIENTED PRORAMMING IN C++ 27

--

Inheritance and Derived Classes

:Inheritances in OO Methodology
Inheritance OOP
OOA OOD
.
)/(child )(derived
) (data members )
(member functions )( ) (
) (parent Base Calss :
)( )(
UML .. ) (
) (
.



"" ""

)( )(
)( )( )(
)( )( )(
)( )(
)(

) (


.
) ( : )( /
.



)(
)(

)(
)(
)(
)(
)(
)(
)(
)(
OBJECT ORIENTED PRORAMMING IN C++ 28

) (
.
) (reuse
.

: C++

++ :
class derived_class : visibility-mode base_calss
{
;Derived calss methods and members
;}
: class .
derived_class .
: .
: visibility-mode ) (private ) (public )(protected
/.
: base_calss .
..........................
...........................
..........................

++++++++++++++++++++++

You might also like