StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover 17. Program to demonstrate inheritance and polymorphism using virtual functions.
Download
  1. C++ Programs list
16. Program to design a `Student` class representing student roll numbers, and a `Test` class (derived from `Student`) representing the scores in various subjects, and a `Sports` class representing the score in sports. Inherit `Sports` and `Test` classes into a `Result` class to add scores and display the final result for a student. : 18. Program to allocate memory dynamically for an object of a given class using the class’s constructor.
C++ Programs list

1.      Program to demonstrate inheritance and polymorphism using virtual functions.

  1. #include <iostream.h>

  2. #include <graphics.h>

  3. #include<conio.h>

  4.  

  5. class Shape {

  6. public:

  7.     virtual void draw() = 0;

  8. };

  9.  

  10. class Line : public Shape {

  11. public:

  12.     void draw() {

  13.                 cout << "Drawing a Line" << endl;

  14.                 // Draw a line

  15.                 line(100, 100, 100, 200);

  16.  

  17.     }

  18. };

  19.  

  20. class Circle : public Shape {

  21. public:

  22.     void draw() {

  23.                 cout << "Drawing a circle" << endl;

  24.                 // Draw a circle

  25.                 circle(200, 200, 50);

  26.  

  27.     }

  28. };

  29.  

  30. class Rectangle : public Shape {

  31. public:

  32.     void draw() {

  33.                 cout << "Drawing a rectangle" << endl;

  34.                 // Draw a rectangle

  35.                 rectangle(100, 250, 300, 350);

  36.  

  37.     }

  38. };

  39.  

  40. int main() {

  41.     int gd = DETECT, gm;

  42.  

  43.     initgraph(&gd, &gm, "C:/TURBOC3/BGI");

  44.  

  45.  

  46.     Shape *shapePtr;

  47.     Line line;

  48.     Circle circle;

  49.     Rectangle rectangle;

  50.  

  51.     shapePtr = &line;

  52.     shapePtr->draw();

  53.     getch();

  54.     shapePtr = &circle;

  55.     shapePtr->draw();

  56.     getch();

  57.     shapePtr = &rectangle;

  58.     shapePtr->draw();

  59.     getch();

  60.     closegraph();

  61.     return 0;

  62. }

16. Program to design a `Student` class representing student roll numbers, and a `Test` class (derived from `Student`) representing the scores in various subjects, and a `Sports` class representing the score in sports. Inherit `Sports` and `Test` classes into a `Result` class to add scores and display the final result for a student. 18. Program to allocate memory dynamically for an object of a given class using the class’s constructor.
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website