0% found this document useful (0 votes)
43 views

A Simple Example of Inheritance.: Using Class Int Public Void Int Int

This document demonstrates multilevel inheritance in C++. It defines three classes - Student as the base class, Test derived from Student, and Result derived from Test. Student stores student name and roll number. Test stores marks in two subjects. Result displays the total marks by inheriting and calling functions from the parent classes. The main function creates a Result object, gets input and calls functions to set data, display output, showing multilevel inheritance in action.

Uploaded by

Candice Chavez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

A Simple Example of Inheritance.: Using Class Int Public Void Int Int

This document demonstrates multilevel inheritance in C++. It defines three classes - Student as the base class, Test derived from Student, and Result derived from Test. Student stores student name and roll number. Test stores marks in two subjects. Result displays the total marks by inheriting and calling functions from the parent classes. The main function creates a Result object, gets input and calls functions to set data, display output, showing multilevel inheritance in action.

Uploaded by

Candice Chavez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

A simple example of inheritance.

#include <iostream> using namespace std; class BaseClass { int i; public: void setInt(int n); int getInt(); }; class DerivedClass : public BaseClass { int j; public: void setJ(int n); int mul(); }; void BaseClass::setInt(int n) { i = n; } int BaseClass::getInt() { return i; } void DerivedClass::setJ(int n) { j = n; } int DerivedClass::mul() { return j * getInt(); } int main() { DerivedClass ob; ob.setInt(10); ob.setJ(4); cout << ob.mul(); } return 0; // load i in BaseClass // load j in DerivedClass // displays 40

Multilevel Inheritance

/********* IMPLEMENTATION OF MULTILEVEL INHERITANCE *********/ #include< iostream.h> using namespace std; class student // Base Class { protected: int rollno; char *name; public: void getdata(int b,char *n) { rollno = b; name = n; } void putdata(void) { cout< < " The Name Of Student \t: "< < name< < endl; cout< < " The Roll No. Is \t: "< < rollno< < endl; } }; class test:public student // Derieved Class 1 { protected: float m1,m2; public: void gettest(float b,float c) { m1 = b; m2 = c; } void puttest(void) { cout< < " Marks In CP Is \t: "< < m1< < endl; cout< < " Marks In Drawing Is \t: "< < m2< < endl; } }; class result:public test // Derieved Class 2 { protected: float total; public: void displayresult(void)

{ total = m1 + m2; putdata(); puttest(); cout< < " Total Of The Two \t: "< < total< < endl; } }; void main() { int x; float y,z; char n[20]; cout< < "Enter Your Name:"; cin>>n; cout< < "Enter The Roll Number:"; cin>>x; result r1; r1.getdata(x,n); cout< < "ENTER COMPUTER PROGRAMMING MARKS:"; cin>>y; cout< < "ENTER DRAWING MARKS:"; cin>>z; r1.gettest(y,z); cout< < endl< < endl< < "************ RESULT **************"< < endl; r1.displayresult(); cout< < "**********************************"< < endl; }

You might also like