290 likes | 417 Views
Chapter 2. Object-Oriented Design and C++ Dr. Youssef Harrath yharrath@uob.edu.bh. Outline. Inheritance Composition Polymorphism Function Overloading Templates. Inheritance.
E N D
Chapter 2 Object-Oriented Design and C++ Dr. YoussefHarrath yharrath@uob.edu.bh
Outline • Inheritance • Composition • Polymorphism • Function Overloading • Templates Dr. Youssef Harrath – University of Bahrain – 2010/2011
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 }; Dr. Youssef Harrath – University of Bahrain – 2010/2011
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). Dr. Youssef Harrath – University of Bahrain – 2010/2011
Inheritance: Example shape “is a” “is a” circle rectangle “is a” square Dr. Youssef Harrath – University of Bahrain – 2010/2011
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) Dr. Youssef Harrath – University of Bahrain – 2010/2011
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). Dr. Youssef Harrath – University of Bahrain – 2010/2011
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; } Dr. Youssef Harrath – University of Bahrain – 2010/2011
Inheritance: Overriding 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
Inheritance: Constructors 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
Inheritance: Constructors 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
Inheritance: Constructors 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
Inheritance: Constructors 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
Inheritance: Constructors derivedClass -first: int -second: double +print(): void +derivedClass() +derivedClass(int, int, int, double) +derivedClass(int, int, char, int, double) baseClass derivedClass UML diagram of the class derivedClass and inheritance hierarchy Dr. Youssef Harrath – University of Bahrain – 2010/2011
Inheritance: Constructors 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; } Dr. Youssef Harrath – University of Bahrain – 2010/2011
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” Dr. Youssef Harrath – University of Bahrain – 2010/2011 16
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. Dr. Youssef Harrath – University of Bahrain – 2010/2011
Composition 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; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011
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; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011
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; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011 20
Composition void personalInfoType::printpersonalInfo() { name.print(); cout<<“ ‘s date of birth is “; bDay.printDate(); cout<<endl; cout<<“and personal ID is “<<personID; } Dr. Youssef Harrath – University of Bahrain – 2010/2011 21
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; } Dr. Youssef Harrath – University of Bahrain – 2010/2011 22
Composition personalInfoType::personalInfoType(string first, string last, int month, int day, int year, int ID) :name(first, last), bDay(month, day, year) { personID = ID; } Dr. Youssef Harrath – University of Bahrain – 2010/2011 23
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. Dr. Youssef Harrath – University of Bahrain – 2010/2011 24
Polymorphism: Function Overloading int larger(int x, int y); char larger(char first, char second); double larger(double u, double v); string larger(string first, string second); int largerInt(int x, int y); char largerChar(char first, char second); double largerDouble(double u, double v); string largerString(string first, string second); Dr. Youssef Harrath – University of Bahrain – 2010/2011 25
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<class Type> function definition; • The syntax for class templates is: template<class Type> class declaration Dr. Youssef Harrath – University of Bahrain – 2010/2011 26
Polymorphism: Function Templates template<class Type> // 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; } Dr. Youssef Harrath – University of Bahrain – 2010/2011 27
Polymorphism: Function Templates template<class Type> // 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 Dr. Youssef Harrath – University of Bahrain – 2010/2011 28
Polymorphism: Class Templates template<class elemType> 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<int> intList; // declares intList to be a list of 100 integers listType<string> stringList; // declares stringList to be a list of 100 strings Dr. Youssef Harrath – University of Bahrain – 2010/2011 29