Posts

Showing posts from February, 2021

C++ Multiple Inheritance Example

  /*WAP according to the specification given below: a)Create a class Teacher with data member 't_id and subjects' and member function for reading and displaying data members. b)Create another class Staff with data members 's_id and position', and member function for reading and displaying data members. c)Derive a class Coordinator from Teacher and Staff and the class must have it's own data member 'department and member function for reading and displaying data memebers) d)Create a two object of Coordinator class and read and display their details. */ #include <iostream> using namespace std; class Teacher{ private:     int t_id;     string subject; public:     void readingTeacherData(){         cout<<"Enter the teacher\'s id :";         cin>>t_id;         cout<<"Enter the subject :";         cin>>subject;     }     void displ...

Calculating area of triangle and rectangle using multiple inheritance in C++

 #include <iostream> using namespace std; class Shape { protected: int l,b,a; public: void getData() { cout<<"Enter the value of l and b:"; cin>>l>>b; } }; class Triangle: public Shape{ public: void display(){ a=(l*b)/2; cout<<"Area of triangle:"<<a; } }; class Rectangle:public Shape{ public:     void display(){     a=l*b;     cout<<"Area of Rectangle:"<<a;     } }; int main() {     Rectangle rectangle;     Triangle triangle;     rectangle.getData();     triangle.getData();     rectangle.display();     cout<<endl;     triangle.display();     return 0; }

Check either a number is armstrong or not using constructor in C++

 #include <iostream> using namespace std; class Arm{ public:     int temp,sum=0,dig,originalNum;     Arm(int num){         originalNum=num;     while(originalNum!=0){     dig=originalNum%10;     sum+=dig*dig*dig;     originalNum=originalNum/10;     }     if(sum==num){         cout<<"This is armstrong number.";     } else{     cout<<"This is not armstrong number.";     }     } }; int main() {     int x;     cout<<"Enter the number: ";     cin>>x;     Arm arm(x);     return 0; }