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...
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”...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
In C Programming Create a payroll program to store and calculate the payroll for a small...
In C Programming Create a payroll program to store and calculate the payroll for a small company as follows: Ask the user for the number of employees and only accept the number if it is between 5 and 40 employees, otherwise prompt the user again and test the input, keep repeating until the user enters an acceptable number. Create an array of floats that is 4 rows and an number of columns equal to the number of employees in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT