Beruflich Dokumente
Kultur Dokumente
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++
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++ 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
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
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
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
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
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
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
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
20
REFERENCES
21
?
??
?
22