Posts

Showing posts from September, 2023

219: Create a C++ program that demonstrates function overriding and dynamic polymorphism in single inheritance. Define a base class "Shape" with a virtual function "displayInfo." Derive classes "Circle" and "Rectangle" from "Shape" and override the "displayInfo" function to provide specific information about each shape.

  // Create a C++ program that demonstrates function overriding and dynamic polymorphism in single inheritance. // Define a base class "Shape" with a virtual function "displayInfo." Derive classes "Circle" and "Rectangle" // from "Shape" and override the "displayInfo" function to provide specific information about each shape. #include < iostream > using namespace std ; class Shape { public :     virtual void displayInfo () { cout << " A closed geometrical figure is known as shape \n " ; } }; class Circle : protected Shape { public :     void displayInfo () override     {         cout << " A circle is a closed two-dimensional figure in which the set of all "               << " the points in the plane is equidistant from a given point called center. \n " ;     } }; class Rectangle : protected Shape { public :     void displayInfo ()...

218: Implement a C++ program to create a basic hierarchy for electronic devices. Create a base class "Device" with attributes like brand and power source. Derive a class "Phone" from "Device" to include features like operating system and screen size.

  // Implement a C++ program to create a basic hierarchy for electronic devices. // Create a base class "Device" with attributes like brand and power source. // Derive a class "Phone" from "Device" to include features like operating system and screen size. #include < iostream > using namespace std ; class Device { private :     string brand , powerSource ; public :     Device ( string b = "" , string p = "" ) { brand = b , powerSource = p ; }     virtual void display_Device (); }; void Device :: display_Device () { cout << " Brand: " << brand << " \n Power Source: " << powerSource << endl ; } class Phone : protected Device { private :     string operating_System , screen_size ; public :     Phone ( string b = "" , string p = "" , string o = "" , string s = "" ) : Device ( b , p )     {...

217: Write a C++ program to model a vehicle hierarchy. Create a base class "Vehicle" with attributes like make and model. Derive classes "Car" and "Motorcycle" from "Vehicle" and add specific attributes like the number of tyres and engine type.

  // Write a C++ program to model a vehicle hierarchy. Create a base class "Vehicle" // with attributes like make and model. Derive classes "Car" and "Motorcycle" from // "Vehicle" and add specific attributes like the number of tyres and engine type. #include < iostream > using namespace std ; class Vehicle { private :     string make , model ; public :     Vehicle ( string ma = "" , string mo = "" ) { make = ma , model = mo ; }     virtual void display_vehicle (); }; void Vehicle :: display_vehicle () { cout << " Make: " << make << " \n Model: " << model << endl ; } class Car : protected Vehicle { private :     int tyres ;     string engine ; public :     Car ( string , string , int , string );     void display_vehicle () override ; }; Car :: Car ( string ma = "" , string mo = "" , int t = 0 , s...

216: Develop a C++ program to manage a list of employees. Create a base class "Employee" with attributes like name and salary. Derive a class "Manager" from "Employee" to include additional attributes like department and team size.

  // Develop a C++ program to manage a list of employees. Create a base class // "Employee" with attributes like name and salary. Derive a class "Manager" // from "Employee" to include additional attributes like department and team size. #include < iostream > using namespace std ; class Employee { private :     string name ;     int salary ; public :     Employee ( string n = "" , int s = 0 ) { name = n , salary = s ; }     virtual void displayEmp () { cout << " Name: " << name << " \n Salary: " << salary << endl ; } }; class Manager : protected Employee { private :     string department ;     int team_size ; public :     Manager ( string n = "" , int s = 0 , string d = "" , int t = 0 ) : Employee ( n , s )     {         department = d , team_size = t ;     }     v...

215: Create a C++ program that uses single inheritance to model a simple shape hierarchy. Implement a base class "Shape" with a virtual function "printArea." Derive classes "Rectangle" and "Triangle" from "Shape" and override the "printArea" function to calculate and display their respective areas.

  // Create a C++ program that uses single inheritance to model a simple shape hierarchy. // Implement a base class "Shape" with a virtual function "printArea." Derive classes // "Rectangle" and "Triangle" from "Shape" and override the "printArea" function to // calculate and display their respective areas. #include < iostream > using namespace std ; class Shape { public :     virtual void printArea () { cout << " Make object of specific shape \n " ;} }; class Rectangle : protected Shape { private :     float length , width ; public :     Rectangle ( float l = 0 , float w = 0 ) { length = l , width = w ; }     void printArea () override { cout << " Area of Rectangle: " << length * width << endl ; } }; class Triangle : protected Shape { private :     float base , height ; public :     Triangle ( float b = 0 , float h = 0 ) ...

214: Develop a C++ program to simulate a basic library system. Create a base class "Book" with attributes like title and author. Derive a class "EBook" from "Book" to include additional attributes like file size and format. Make display function with same name as in parent class

// Develop a C++ program to simulate a basic library system. Create a base class "Book" with // attributes like title and author. Derive a class "EBook" from "Book" to include additional // attributes like file size and format. // Make display function with same name as in parent class #include < iostream > using namespace std ; class Book { private :     string title , author ; public :     Book ( string t = "" , string a = "" ) { title = t , author = a ; }     virtual void displayBook () { cout << " Title: " << title << " \n Author: " << author << endl ; } }; class EBook : protected Book { private :     int file_size ;     string format ; public :     EBook ( string t = "" , string a = "" , int fs = 0 , string f = "" ) : Book ( t , a ) { file_size = fs , format = f ; }     void disp...

213: Implement a C++ program to model a simple banking system. Create a base class "Account" with attributes like account number and balance. Derive a class "SavingsAccount" to include features like interest calculation and deposit/withdrawal.

// Implement a C++ program to model a simple banking system. Create a base class "Account" with // attributes like account number and balance. Derive a class "SavingsAccount" to include features // like interest calculation and deposit/withdrawal. #include < iostream > using namespace std ; class Account { private :     int account_num , balance ; public :     Account ( int a = 0 , int b = 0 ) { account_num = a , balance = b ; }     void display_Account () { cout << " Account number: " << account_num << " \n Balance: " << balance << endl ; }     int getBalance () { return balance ; } }; class SavingsAccount : protected Account { private :     float interest_rate ;     int years ; public :     SavingsAccount ( int a = 0 , int b = 0 , float i = 0.0 , int y = 0 ) : Account ( a , b ){ interest_rate = i , years = y ;} ...
Virtual and Override: Virtual: You use 'virtual' keyword with the function in the base class that is to be overridden in the derived class Override:  You use 'override' keyword with the function that is being overridden in the derived class. This shows an error during compile-time when the names of virtual and override functions are not same. Example: class base { public :     virtual void print () { cout << " This is a base function \n " ; } }; class derived : public base { public :     void print () override { cout << " This is an overridden function \n " ; } }; Practice 1: // Write a C++ program to demonstrate single inheritance by creating a base class "Circle" with a member // function to calculate the area of circle and a derived class "new_Circle" to calculate the area of a circle // if radius is positive and print a messege if radius is negative. #include < iostream > using namespac...

212: Build a class representing a university student with private data members for name, ID, and a static data member for the total number of students. Implement getter and setter functions for name and ID, and a static getter function to retrieve the total number of students. Use default constructors

  // Build  a  class  representing  a  university  student  with  private  data  members  for // name, ID, and a static data member for the total number of students. Implement // getter  and  setter  functions  for  name  and  ID,  and  a  static  getter  function  to // retrieve the total number of students. Use default constructors #include < iostream > using namespace std ; class Student { private :     static unsigned int count ;     string name , id ; public :     // constructor     // Student() : name(""), id("") { count++; }     // Student(string x, string y) : name(x), id(y) { count++; }     // set data     void setData ();     // print     void printStudent ();     static int printTotal_students (); }; unsigned int Student :: count = ...