Question

In: Computer Science

C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...

C++ Programming

Create a C++ program

program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and private as appropriate. The derived classes will override the virtual function(s) in the base class as necessary. The derived classes extend the base class by having at least one member that the base class does not have. The derived classes will access private members of the base class by using the base classes set and get functions.

Please type code and include screenshots of output of the code. Also code must be well commented.

Solutions

Expert Solution

Code

#include<iostream>
#include<string>
using namespace std;

//base class Names shape and having one ptivate member type string name and one pure virtual function calculate area
class Shape
{
private :
   string name;
public:
   Shape(string);
   void setName(string);
   string getNmae();
   virtual double calculateArea()=0;
};
Shape::Shape(string name)
{
   this->name=name;
}
void Shape::setName(string nm)
{
   name=nm;
}
string Shape::getNmae()
{
   return name;
}

//class Rectangle derived from the class shape and have two private member typed double length and width
class Rectangle:public Shape
{
private:
   double length,widht;
public:
   Rectangle(string,double,double);
   double calculateArea();
};
Rectangle::Rectangle(string name,double l,double w):Shape(name)
{
   length=l;
   widht=w;
}
double Rectangle::calculateArea()
{
   double area=length*widht;
   return area;
}

//class Circle derived from the class shape and have one private member typed double radius
class Circle:public Shape
{
private:
   double radius;
public:
   Circle(string,double);
   double calculateArea();
};
Circle::Circle(string name, double r):Shape(name)
{
   radius=r;
}
double Circle::calculateArea()
{
   double area=3.14*radius*radius;
   return area;
}

//class Triangle derived from the class shape and have two private member typed double base and height
class Triangle:public Shape
{
private:
   double base,height;
public:
   Triangle(string,double,double);
   double calculateArea();
};

Triangle::Triangle(string name,double b,double h):Shape(name)
{
   base=b;
   height=h;
}

double Triangle::calculateArea()
{
   double area=0.5*base*height;
   return area;
}

int main()
{
   Rectangle r1("Recangle",10,20);//careate object of the Recangle class
   Triangle t1("Triangle",10,20);//create object of Triangle class
   Circle c1("Circle",100);//create object of Circle class

   cout<<"Shape :"<<r1.getNmae()<<" and area :"<<r1.calculateArea()<<endl;
   cout<<"\nShape :"<<t1.getNmae()<<" and area :"<<t1.calculateArea()<<endl;
   cout<<"\nShape :"<<c1.getNmae()<<" and area :"<<c1.calculateArea()<<endl<<endl;
   return 1;
}

ouptut

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

C++ polymorphism. Create a program that issues a quiz through the terminal. The quiz will ask...
C++ polymorphism. Create a program that issues a quiz through the terminal. The quiz will ask a variety of C++ questions to the user and prompt them for a response. The response will differ based on the type of question, such as true or false, multiple-choice, or fill in the blank. Part 1: Designing the Quiz Design this program using a polymorphic vector of questions. Create a Question base class that will serve to point to three different derived classes:...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
C++ program: Create a Date class that contains three members: the month, the day of the...
C++ program: Create a Date class that contains three members: the month, the day of the month, and the year, all of type int. The user should enter a date in the format 12/31/2001, store it in an object of type Date, then retrieve the object and print it out in the same format. Next create an employee class. The member data should comprise an employee number (type int) and the employee’s compensation (in dollars; type float). Extend the employee...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT