IN THE NAME OF GOD
OOP in
C++
Maysam Rajaati Farid Bekran
11/27/2008
Tabriz University - Iran 1
TABLE OF CONTENTS
1. History
2. Philosophy of C++ & increments of C
3. Classes in C++
4. Objects in C++
5. Access Specifiers
6. Class & Object memory allocation
7. Static data (In classes)
8. Inheritance(Single)
9. Inheritance(multiple)
10.INCLUSIVE CLASSES
11.POLYMORPHISM
12.COMPILERS
2
HISTORY
It was developed by Bjarne Stroustrup in1979 at Bell Labs.
As an enhancement to the C programming language and originally named "C
with Classes".
It was renamed to C++ in 1983.
C Simula ADA83 Algol68 SmallTalk CLU ML
C++
C# JAVA ADA95 PHP Perl D
3
PHILOSOPHY OF C++ &
INCREMENTS OF C
C++ is designed to be a statically typed general-purpose language that is as
efficient and portable as C
C++ is designed to directly and comprehensively support multiple programming
styles (procedural programming, data abstraction, object-oriented programming,
and generic programming)
C++ avoids features that are platform specific or not general purpose
Virtual Multiple
class
C+
functions inheritance
C
Exception
handling
Templates
Operator
overloading
+
4
CLASSES IN C++
Classes are User defined types.
Classes are for Encapsulate our data.
Classes hold both data and function on data.
CPolygon
class CPolygon
{
private: int Width; int Height; // You can remove “private”
public:
Cpolygon() // Has no return value
{ }
int Area()
{ }
void Draw()
{ }
~Cpolygon() // Has no Parameter , Has no return value
{ }
}; 5
OBJECTS IN C++
Objects of Class
Definition of class
Some Data Some Data
Some Data
1000 10230
Definition of a class only is the class Specification.
Object of a class allocate real memory for members.
Example:
…
Cpolygon obj1; // call the user defined constructor
Obj1.Draw(); // draw the obj1 to screen
cout << Obj1.Area(); //prints the area of polygon obj1
// if scope ends the destructor called
… 6
ACCESS SPECIFIERS
Example 1 :
Class Cpolygon …
Cpolygon obj1;
cout << Obj1.Width; // ERROR!
private obj.Draw(); //True!
…
NOT ALLOWED
protected
Exam ple 2 :
public …
class CPolygon
{
void Draw()
{
cout << Width <<
Height;
Cpolygon objB // True
…
} 7
CLASS & OBJECT MEMORY
ALLOCATION
Object 1 Object 2 Object 3
Data 1 Data 1 Data 1
Data 2 Data 2 Data 2
Function 1
Function 2
8
STATIC DATAS(IN CLASSE)
Object 2
Object 1 Object 3
Static members use the Normal data Normal data Normal data
access Secifiers of other
Data 1 Data 1 Data 1
members.
Data 2 Data 2 Data 2
Class A
{
… //public
Static data
Static int x;//declaration
Data 3
…
}; Data 4
int A::x = 0;//definition
Main()
{
cout << A::x; // access to static member
… 9
}
9
STRUCTURES & CLASSES
Difference between class and struct
Structure members are by default public but in class members
are by default private.
Structures inherited publicly by default but classes inherited
privately.
10
INHERITANCE
A
Single Inheritance
B C
Multiple inheritance B C
A 11
INHERITANCE(SINGLE) Derived Class
Base Class
Feature A
Base Class
Defined in
Feature A Feature B
Arrow means Derived from
Feature B Feature C
Derived Class
Defined in
Feature D
Feature C
class CPolygon
{
protected : int Wedth; int Height; CPolygon
public:
int Area()
{ }
int Draw() CRectangle
{ }
}; 12
class CRectangle : public CPolygon
{ };
INHERITANCE(SINGLE)
Access Specifier Class Base
Base class has no access
private
to derived class
Class Derived protected
private
public
protected
Base objB
public
Derived objD
13
INHERITANCE(SINGLE)
Constructors of derived class
If derived class has no
constructor compiler use
suitable constructor of
Class Cpolygon Base class.
{
protected : int Width; int Height;
public:
Cpolygon(int para1 , int para2) : Width(para1), Height(para2)
{ }
};
Class CRectangle: public Cpolygon
{
int ColorNmber;
public:
Crectangle(int para1 , int para2, int para3) :Cpolygon(para1, para2),ColorNumber(para3)
{ }
}
14
INHERITANCE(SINGLE)
Public & Private Inheritance class A
private
protected
Class B : public A
Class C: private A
public
NOT ALLOWED
private
private
protected
protected
A objA
public
public
Public & Private Inheritance
are methods that control
Objects accessibility to
B objB
parent public members. C objC 14
INHERITANCE(SINGLE)
Overloading functions in base and derived class
Class Cpolygon
{
…
public:
void Draw()
{ cout << “Drawing Cpolygon”<< “\n”;}
};
Class CRectangle : public Cpolygon
{
…
public:
void Draw()
{ // use the suitable draw steps }
}
Inplemention
…
CRectangle objREC; Cpolygon objPOL;
objREC.Draw(); objPOL.Draw();
…
15
INHERITANCE(MULTIPLE)
Base class A Base class B
Derived class C
class A Overloading functions in this kind
{ }; of inheritance is like single
class B inheritance.
{ };
class C : public A , public B
{ };
16
INHERITANCE(MULTIPLE)
Ambiguity
class A
{
public:
void show() { cout << “in A class”;};
};
class B
{
public:
void show() { cout << “in B class”;};
};
class C : public A , public B
{ // this class has no method with show() signature };
Implementation :
…
C object;
object.show(); // compile error
Object.A::show(); // True 17
Object.B::show(); // True
…
INCLUSIVE CLASSES
Class Bird
Class Wing
Class Wing
class wing
{ };
class bird
Class Hen {
…
wing obj; //bird has wing
Class Wing …
};
class hen : public bird
{ }; // hen derived from bird
18
POLYMORPHISM
Polymorphism
Dynamic Static
Uses virtual function Uses function overloading
members Compile time
Run-time
class Cpolygon
{
…
virtual void Draw() { } If you don’t use virtual keyword on base
… class function, call line of “for” statement
} will call the base class function in every
Cpolygon* ptrs[100]; step.
… // fill the ptrs[100]
for(int i=0 ; i<100 ; i++)
ptrs[i] -> draw();
19
COMPILERS
Dev C++ (open source) - Free
Microsoft Visual studio 2003 / 2005 / 2008 / 2010 - commercial
Borland C++ builder - commercial
Borland Turbo C++ explorer 2006 (Japanese , French , English ) - Free
Apple C++
IBM C++
Intel C++ for Linux
Sun Studio
20
REFERENCES
• Lefor, Robert W. Object Oreinted Programming in C++, 3rd ed.
• pratt, terans w. Programming language.
• www.wikipedia.com
• www.cplusplus.com
21
?
??
?
22