IN THE NAME OF GOD
OOP C++
in
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
class
Virtual functions
Multiple inheritance
Templates
Operator overloading
C Exception handling
C+ + 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 Class Cpolygon private
public
Cpolygon objB
NOT ALLOWED
protected
Example 1 : … Cpolygon obj1; cout << Obj1.Width; // ERROR! obj.Draw(); //True! …
Exam ple 2 : … class CPolygon { void Draw() { cout << Width << Height; // 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
Static members use the access Secifiers of other members.
Object 3
Normal data
Normal data
Data 1
Data 1
Data 1
Data 2
Data 2
Data 2
Class A { … //public Static int x;//declaration … }; int A::x = 0;//definition Main() { cout << A::x; // access to static member … }
Normal data
Static data Data 3 Data 4
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
Multiple inheritance
C
B
C A
11
INHERITANCE(SINGLE) Base Class
Feature B
Arrow means Derived from
class CPolygon { protected : int Wedth; int Height; public: int Area() { } int Draw() { } }; class CRectangle : public CPolygon { };
Feature D
Defined in Derived Class
Feature C
Feature C
Defined in Base Class
Feature A
Feature A
Feature B
Derived Class
CPolygon
CRectangle
12
INHERITANCE(SINGLE) Access Specifier Base class has no access to derived class Class Derived private
Class Base
private protected
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 Base class.
Class Cpolygon { 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
protected
A objA
NOT ALLOWED
private
public
B objB
private
protected
public
Public & Private Inheritance are methods that control Objects accessibility to 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 B
Base class A
Derived class C
class A { }; class B { }; class C : public A , public B { };
Overloading functions in this kind of inheritance is like single inheritance.
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 Object.B::show(); // True …
17
INCLUSIVE CLASSES Class Bird Class Wing Class Wing
Class Hen Class Wing
class wing { }; class bird { … wing obj; //bird has wing … }; class hen : public bird { }; // hen derived from bird
18
POLYMORPHISM Polymorphism
Dynamic Uses virtual function members Run-time class Cpolygon { … virtual void Draw() { } … }
Cpolygon* ptrs[100]; … // fill the ptrs[100] for(int i=0 ; i<100 ; i++) ptrs[i] -> draw();
Static Uses function overloading Compile time
If you don’t use virtual keyword on base class function, call line of “for” statement will call the base class function in every step.
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