Object Oriented Programming

1 of
Published on Video
Go to video
Download PDF version
Download PDF version
Embed video
Share video
Ask about this video

Page 1 (0s)

Object Oriented Programming. LECTURE 7 Math.262. Dr. Nermeen Elhamy.

Page 2 (8s)

The Default Copy Constructor - Itis another way to initialize an object: - Used to initialize an object with another object of the same type. - No need to create a special constructor for this; one is already built into Class : Distance Feet Inches Distance ( ) —Distance ( ) Object 1 Feet = 5 Inches = 3.5 Object 2 Feet -5 Inches = 3.5.

Page 3 (24s)

The Default Copy Constructor class Distance //EngIish Distance class private: int feet; float inches; public: Distance() : feet(O), inches(O.O) Distance(int ft, float in) : feet(ft), inches(in) int main() Distance d 1; Distance dist2 (II, 6.25); Distance dist3 (dist2); Distance dist4 = dist2;.

Page 4 (38s)

private : int feet; float inches; public: Distance ( ) ; Distance (int f, float i) ; void setDistance (int f, float i) ; void print ( ) ; &Distance ( ) ;.

Page 5 (48s)

void Distance: : setDistance (int f; inches = 1 €void Distance: :print ( ) f, float cout << "Feet = feet i) " inches inch.

Page 6 (1m 3s)

3 4 5 6 7 8 9 10 11 12 13 14 15 16 E#include "pch.h" #include <iostream> #include "Distance.h" Sint main() Distance dl; Distance d2(5, 3.5) Distance d3 (d2) ; d3 .print ; d3 . setDistance (7 , 4.25); Distance d4 = d3; d4 .print ( ) ; Microsoft Visual Studio Console Feet = S eet = 7 inches = 3.5 inches = 4.25 : SOneDriue — •class .exe (process 848D exited with code ress any key to close this window ..

Page 7 (1m 21s)

Exercise. Write a class for a book class that contains the following members : 1- The name of the book 2- his number 3- Its price 4- the author’s name 5- The number of the book 6- count And contains the following functions: 1- A default constructor and a mediator's constructor. 2-Properties of get && set 3- A function to search for a book by its name. 4- A function for selling a book by the book number, and this function returns the amount of the remaining books.

Page 8 (1m 44s)

Static Class Members • Static fields and static methods do not belong to a Single instance ofa class. • A static data item is useful when all objects of the same class must share a common information. • Its lifetime is the entire program. It continues to exist even if there are no objects of the class. • To invoke a static method or a static field, use the class name the instance name..

Page 9 (2m 4s)

1 &tal 2 Autornatic data datal Statk data ot*ect 3 mtornatk data datal.

Page 10 (2m 11s)

OtUetr otueS otuex 10.

Page 11 (2m 17s)

Ex. Class Car {. private: string maker; int model; string color; int count =OI; public: void setMaker (string m) ; string getMaker ( ) ; void setMode1 (int m) ; int getMode1 ( ) ; int Getcarscount ( ) ; Car (string m, int mo, string c) ;.

Page 12 (2m 29s)

:Car (string m, •Car : count++ ; scar: FCar count-- ; int mo, string c) :maker (m) ,model (mo).

Page 13 (2m 39s)

E#include "pch.h" #include <iostream> I *include "Car . h" Sint main() Car cl; cout<<"Counter reached "<< cl .GetCarscount ( ) ; Car c2; cout << "Counter reached " << c2.GetCarscount ( ).

Page 14 (2m 55s)

using namespace std; —class Car private : string maker; int model; string color; static int countl public : void setMaker (string m) ; string getMaker () ; void setMode1 (int int getMode1 ( ) ;.

Page 15 (3m 7s)

Firstc++class 1 2 3 4 5 6 7 8 9 10 (Global Scope) s#include "pch.h" *include "Car. h" #include<iostream> using namespace std; int car: :count = scar: :car ( ) :maker ("Toyota") , model (2019) , color ("White") count++;.

Page 16 (3m 22s)

Then , Run the program. Distance.h Firste + class 4 5 6 7 8 9 Distance.cpp pch.cpp Firstc++class.cpp (Global Scope) include "pch.h" #include <iostream> #include "Car . h" Sint main() Car cl; cout<<"Counter reached 10 11 12 13 14 15 1 1 1 Car c2; cout << car c3; "Counter reached " cl . Getcar c2.GetC ' Microsoft Visual Studio Debug Console Counter reached 1 Counter reached 2 tountep reached 3.

Page 17 (3m 38s)

Static Methods • Static methods are convenient because they may be called at the class level. They are typically used to create utility classes. • Static methods may not communicate with Instance fields, only static fields..

Page 18 (3m 55s)

Ex. I Sot•.nion Explorer Search Solution Explorer (Ctrl•; p • Solution (l References G External Dependencies Header Files Calculator.h Car.h Distance.h Rectangle.h Resource Files Source files +4 Calculator.cpp +4 Qr.cpp Calculator.cpp• Firstc• • class 2 3 4 5 6 8 9 10 11 12 13 *include Distance.cpp c <string> pchcpp pch.h • Calculator hrstc• • class.( using namespace std; -class calculator public: static int add(int numl, int num2) ; static int mutiply (int numl, int num2) ; static lint subtract (int numl, int num2) ; Calculator () ; &CaIcuIator () ;.

Page 19 (4m 16s)

How Calling utility class (static method). 4 5 6 7 8 9 10 11 12 13 14 8 #include "pch. h" # include <iostream> #include "Calculator . h" [Sint main ( ) Calculator: • .add(2, 5); :mutip1y(3, 5) ; Calculator: Calculator: : subtract (10, 2) ;.

Page 20 (4m 31s)

Inheritance. What is Inheritance? • Inheritance is probably the most powerful feature of object-oriented programming, after classes themselves. Inheritance is the process of creating new classes, called derived classes, from existing or classes • The derived class inherits all the capabilities of the base class but can add its own features. And the base class is unchanged by this process..

Page 21 (4m 33s)

Base dass Featwe A &rived frun das in Defiræd in base dass.

Page 22 (4m 48s)

"'ith Inheritance Person —Last name —First name —Address —Home phone +Ugxiate address() Employee —Hire date —pay grade +Ugxiate pay grade() Customer —Last contact date last contact date().

Page 23 (4m 52s)

Class • > - Age - Nationality Get _ Age ( ) Get _ a&fress ( ) •Set _ Nationality •Get_ Nationality ) Sub Studen t - Study _ Level • ialization GPA • Get _ ) • Set _ Specialization ( ) • Get _ Specialization ( ) • Set_GPA ( e <Sub Class* • - Salary • Rank • Set _ Salary ( ) Get _ Salary ) Set_rank ( ) Get _ rank ) Set _ Job ( ) Get_Job ) < «Sub Class* Salaried Employee - Deductions • Set _ Working_hours ( ) + Get _ Working_hours ( ) Get _ Houre_Rate ( ) Sub Class* Hourly Employee - Working _ Hours - _ rate • Set _ Working. hours ( ) • Get _Working _ hours ( ).

Page 24 (5m 13s)

Each derived class, in turn, can become a base class for a future derived class. Inheri- tance can be either single inheritance or multiple inheritance. In single inheritance, the derived class is derived from a single base class; in multiple inheritance, the derived class is derived from more than one base class. This chapter concentrates on single inheritance..

Page 25 (5m 24s)

The protected Members. A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes..

Page 26 (5m 51s)

To define a derived class ,. class derived-class : access- specifier base-class Class derived-class : < inheritance type > base-class.

Page 27 (6m 3s)

Type of Inheritance. When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied : • Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but be accessed through calls to can the public and protected members of the base class..

Page 28 (6m 8s)

Type of Inheritance. • deriving from a protected base Protected Inheritance: When class, public and protected members of the base class become protected members of the derived class..

Page 29 (6m 17s)

Access Control and Inheritance. A derived class can access all the non-private members of its base class . Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class . We can summarize the different access types according to - who can access them , in the following way:.

Page 30 (8m 42s)

Example. Consider a base class Shape and its derived class Rectangle as follows: class Shape void setHeight ( int h) } height = h; ;.

Page 31 (9m 2s)

int main(void) } Rectangle Rect ; Rect.setWidth (5); Rect.setHeight (7); // Print the area of the object. cout << "Total area: " << Rect.getArea () << endl ; return 0; {.

Page 32 (9m 10s)

Multiple Inheritance. A C++ class can inherit members from more than one class and here is the extended syntax : class derived-class : access baseA , access baseB , .... Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above..

Page 33 (9m 59s)

*include using namespace std; / / Base class Shape class Shape public : void setWidth( w) width void setHeight ( int height = h; protected : int width; int height;.

Page 34 (10m 14s)

int main(void) Rectangle Rect; int area; Rect . setWidth(5); Rect . setHeight(7); area = Rect . getArea( ) ; // Print the area of the object. Rect . getArea() << endl; cout << "Total area: ' // Print the total cost of painting cout << "Total paint cost: $" << Rect. getCost(area) << endl; return 0;.

Page 35 (10m 57s)

A derived class inherits all base class methods with the following exceptions: • Constructors, destructors and copy constructors of the base class. • Overloaded operators of the base class. • The friend functions of the base class,.