1 / 18

Lab 4: Inheritence

Lab 4: Inheritence. Presented By: Nazia Hossain Lecturer, Stamford University. Inheritence. One of the most important concepts in object-oriented programming is that of inheritance.

roy
Download Presentation

Lab 4: Inheritence

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 4: Inheritence Presented By: Nazia Hossain Lecturer, Stamford University

  2. Inheritence • One of the most important concepts in object-oriented programming is that of inheritance. • Inheritance allows us to define a class in terms of another class, that provides an opportunity to reuse the code functionality and fast implementation time. • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. • This existing class is called the base class, and the new class is referred to as the derived class.

  3. Base & Derived Classes • To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form: • class derived-class: access-specifierbase-class • Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. • If the access-specifier is not used, then it is private by default.

  4. Consider a base class Shape and its derived class Rectangle as follows: #include <iostream> using namespace std; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; };

  5. // Derived class class Rectangle: public Shape { public: intgetArea() { return (width * height); } }; 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; } What is the output of this program? Ans:

  6. Access Control and Inheritance:

  7. 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.

  8. Type of Inheritance:

  9. Multiple Inheritances • A C++ class can inherit members from more than one class and here is the extended syntax: • class derived-class: access baseA, access baseB....

  10. Example of Multiple Inheritance #include <iostream> using namespace std; // Base class Shape class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; };

  11. // Base class PaintCost class PaintCost { public: intgetCost(int area) { return area * 70; } }; // Derived class class Rectangle: public Shape, public PaintCost { public: intgetArea() { return (width * height); } };

  12. int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; } What is the output of this program? Answer:

  13. Hybrid Inheritance • Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances. • The following is a C++ Program to for Calculating the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.

  14. #include<iostream> #include<conio.h> #include<stdio.h> #include<string.h> using namespace std; class student_id { //c-madeeasy.blogspot.com intrno; char name[20]; public: void read_id() { cout<<"\n\nEnter the Name of the Student : "; gets(name); cout<<"\n\nEnter the Roll No. : "; cin>>rno; } void display_nr() { cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : "; puts(name); cout<<"\n\nROLL NO. : "<<rno; } };

  15. class marks:publicstudent_id { public: inti,mark[3]; void read_m() { read_id(); for(i=0;i<3;i++) { cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : "; cin>>mark[i]; } } void display_m() { display_nr(); cout<<"\n\n\tMarks Secured "; for(i=0;i<3;i++) cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i]; } };

  16. class sports { public: intsm; void read_sportm() { cout<<"\n\nEnter the marks in SPORTS out of 10 : "; cin>>sm; } };

  17. class percentage:publicmarks,public sports { public: float total,prcntge; void calculate() { read_m(); read_sportm(); total=0; for(i=0;i<3;i++) { total+=mark[i]; } total+=sm; prcntge=(total/310)*100; } void display_totp() { display_m(); cout<<"\n\nTOTAL = "<<total; cout<<"\n\nPERCENTAGE = "<<prcntge; } };

  18. int main() { int cont; percentage pc; do { pc.calculate(); pc.display_totp(); cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)"; cin>>cont; }while(cont==1); getch(); return 0; }

More Related