Download presentation
Presentation is loading. Please wait.
Published byLorin Johnson Modified over 9 years ago
1
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh
2
Outline 1. Inheritance 2. Composition 3. Polymorphism Function Overloading Templates 2 Dr. Youssef Harrath – University of Bahrain – 2010/2011
3
Inheritance Inheritance is an Object-Oriented Design concept which relates many classes together to allow specific classes using members of general classes. It’s a kind of a hierarchy relation between different classes of different levels. The general syntax to define a derived class is: Where memberAccessSpecifier is public, protected, or private (by default). class className: memberAccessSpecifier baseClassName { member list }; 3 Dr. Youssef Harrath – University of Bahrain – 2010/2011
4
Inheritance private members of the base can not be directly accessed by members of the derived class. public members of a base class can be inherited either as public or private members by the derived class. The derived class can include additional data/functions The derived class can redefine the public functions of the base class. All the data/function members of the base class are members of the derived class (except the redefined functions). 4 Dr. Youssef Harrath – University of Bahrain – 2010/2011
5
Inheritance: Example shape circlerectangle square “is a” 5 Dr. Youssef Harrath – University of Bahrain – 2010/2011
6
Inheritance: Example Suppose that we have the class shape: class circle: public shape { // public inheritance. }; class circle: private shape //by default { // private inheritance. }; // the public members of shape become private members of circle (any object of type circle can not directly access these members) 6 Dr. Youssef Harrath – University of Bahrain – 2010/2011
7
Inheritance: Overriding Public functions of the base class can be redefined (overrided) in a derived class (subclass). The redefined function in the subclass must have the same name, number, and types of parameters (formal parameter list). If both functions in the base class and in the derived class have the same name but different formal parameter list, this is called overloading (allowed). 7 Dr. Youssef Harrath – University of Bahrain – 2010/2011
8
Inheritance: Overriding class baseClass { public: void print () ; private: int u, v; // not accessible directly in the derived class char ch; }; void baseClass::print() { cout <<"Base Class: u = "<<u <<", v = "<<v <<" ch = "<<ch<<endl; } 8 Dr. Youssef Harrath – University of Bahrain – 2010/2011
9
Inheritance: Overriding 9 class derivedClass: public baseClass { public: void print () ; private: int first; double second; }; void derivedClass::print() { baseClass::print(); //call of the public print function of the // base class to print the private members cout <<"Derived Class: first = "<<first <<", second = "<<second<<endl; } Dr. Youssef Harrath – University of Bahrain – 2010/2011
10
Inheritance: Constructors 10 class baseClass { public: void print (); baseClass(); // constructor 1 baseClass(int x, int y); // constructor 2 baseClass(int x, int y, char w); // constructor 3 private: int u, v; // not accessible directly in the derived class char ch; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011
11
Inheritance: Constructors 11 baseClass -u: int -v: int -ch: char +print(): void +baseClass() +baseClass(int, int) +baseClass(int, int, char) UML diagram of the class baseClass Dr. Youssef Harrath – University of Bahrain – 2010/2011
12
Inheritance: Constructors 12 void baseClass::print() { // see slide 8 } baseClass:: baseClass() // constructor 1 { u = 0; v = 0; ch = ‘*’; } baseClass:: baseClass(int x, int y) // constructor 2 { u = x; v = y; ch = ‘*’; } baseClass:: baseClass(int x, int y, char w) // constructor 3 { u = x; v = y; ch = w; } Dr. Youssef Harrath – University of Bahrain – 2010/2011
13
Inheritance: Constructors 13 class derivedClass: public baseClass { public: void print (); derivedClass(); // constructor 1 derivedClass(int x, int y, int one, double two); // constructor 2 derivedClass(int x, int y, char w, int one, double two); // constructor 3 private: int first ; double second; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011
14
Inheritance: Constructors 14 derivedClass -first: int -second: double +print(): void +derivedClass() +derivedClass(int, int, int, double) +derivedClass(int, int, char, int, double) UML diagram of the class derivedClass and inheritance hierarchy Dr. Youssef Harrath – University of Bahrain – 2010/2011 baseClass derivedClass
15
Inheritance: Constructors Dr. Youssef Harrath – University of Bahrain – 2010/2011 15 void derivedClass::print() { // see slide 9 } derivedClass::derivedClass() // default constructor { // default constructor of the base class will be invoked first = 0; second = 0; } derivedClass::derivedClass(int x, int y, int one, double two) :baseClass(x, y) { first = one; second = two; } derivedClass::derivedClass(int x, int y, char w, int one, double two) :baseClass(x, y, w) { first = one; second = two; }
16
Inheritance: Header files A C++ programming practice is to create header files (.h extension) for the classes Suppose that baseClass is placed in the header file baseClass.h, the definition of the class derivedClass in a new file must start with #include “baseClass.h” 16 Dr. Youssef Harrath – University of Bahrain – 2010/2011
17
17 Composition Composition is another way to relate two classes. Composition is used for objects that have a “has-a” relationship to each other. In order to facilitate the building of complex classes from simpler ones, C++ allows us to do object composition in a very simple way — by using classes as member variables in other classes.
18
Dr. Youssef Harrath – University of Bahrain – 2010/2011 18 class personType { public: void print(); void setName(string first, string last); void getName(string& first, string& last); personType(string first, string last); private: string firstName; string lastName; }; Composition
19
Dr. Youssef Harrath – University of Bahrain – 2010/2011 19 Composition class dateType { public: void printDate(); void setDate(int month, int day, int year); void getDate(int& month, int& day, int& year); dateType(int month, int day, int year); private: int dMonth; int dDay; int dYear; };
20
Dr. Youssef Harrath – University of Bahrain – 2010/2011 20 Composition class personalInfoType { public: void printpersonalInfo(); void setpersonalInfo(string first, string last, int month, int day, int year, int ID); personalInfoType(string first, string last, int month, int day, int year, int ID); private: personType name; dateType bDate; int personID; };
21
Dr. Youssef Harrath – University of Bahrain – 2010/2011 21 Composition void personalInfoType::printpersonalInfo() { name.print(); cout<<“ ‘s date of birth is “; bDay.printDate(); cout<<endl; cout<<“and personal ID is “<<personID; }
22
Dr. Youssef Harrath – University of Bahrain – 2010/2011 22 Composition void personalInfoType::setpersonalInfo(string first, string last, int month, int day, int year, int ID) { name.setName(first, last); bDay.setDate(month, day, year); personID = ID; }
23
Dr. Youssef Harrath – University of Bahrain – 2010/2011 23 Composition personalInfoType::personalInfoType(string first, string last, int month, int day, int year, int ID) :name(first, last), bDay(month, day, year) { personID = ID; }
24
Dr. Youssef Harrath – University of Bahrain – 2010/2011 24 Polymorphism Polymorphism is one of the principles of Object-Oriented Design (OOD). Polymorphism will be discussed via overloading and then via templates. Overloading is a OOD concept to allow the programmer defining operators/functions with same name but different formal parameter list. Templates enable the programmer to write generic codes for related function and classes.
25
Dr. Youssef Harrath – University of Bahrain – 2010/2011 25 Polymorphism: Function Overloading int largerInt(int x, int y); char largerChar(char first, char second); double largerDouble(double u, double v); string largerString(string first, string second); int larger(int x, int y); char larger(char first, char second); double larger(double u, double v); string larger(string first, string second);
26
Dr. Youssef Harrath – University of Bahrain – 2010/2011 26 Polymorphism: Templates Templates are very powerful features in C++. Templates enable the user writing a single code segment for a set of related functions: function template, and for related classes: class template. The syntax for function templates is: template function definition; The syntax for class templates is: template class declaration
27
Dr. Youssef Harrath – University of Bahrain – 2010/2011 27 Polymorphism: Function Templates template // Part 1 Type larger(Type x, Type y); void main() { cout<<“Line 1: Larger of 5 and 6 = “<<larger(5, 6)<<endl; cout<<“Line 2: Larger of A and B = “<<larger(‘A’, ‘B’)<<endl; cout<<“Line 3: Larger of 5.6 and 3.2 = “<<larger(5.6, 3.2)<<endl; string s1 = “Hello”, s2 = “Happy”; cout<<“Line 4: Larger of “<<s1<<“ and “<<s2<<“ = “<<larger(s1,s2)<<endl; }
28
Dr. Youssef Harrath – University of Bahrain – 2010/2011 28 Polymorphism: Function Templates template // Part 2 Type larger(Type x, Type y) { if( x >= y) return x; else return y; } Output Line 1: Larger of 5 and 6 = 6 Line 2: Larger of A and B = B Line 3: Larger of 5.6 and 3.2 = 5.6 Line 4: Larger of Hello and Happy = Hello
29
Dr. Youssef Harrath – University of Bahrain – 2010/2011 29 Polymorphism: Class Templates template class listType { public: bool isEmpty(); bool isFull(); void search(const elemType& searchItem, bool& found); void insert(const elemType& newElement); private: elemType list[100]; int length; }; … listType intList; // declares intList to be a list of 100 integers listType stringList; // declares stringList to be a list of 100 strings
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.