Question

C++

Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) .

Assignment:

1. Define an abstract base class called BasicShape. The BasicShape class should has the following members: (Reference: Fig 12.9 and 12.10) a) Protected Member Variable: area (a double used to hold the shapes area) b) Private Member Variable: name (a string to indicate the shapes type) c) Constructor and Public Member Functions BasicShape double a, string n): A constructor that sets value of member area with a and member name with 1n calcAreal: This public function should be a pure virtual function. print0: A pubic virtual function that prints the value of data member area getName):A pubic function that returns the value of data member name 2. Define a class named Circle. It should be derived from the BasicShape class It should have the following members: (Reference: Fig 12.11 and 12.12) a) Private Member Variable: radius (a double used to hold the circles radius) b) Constructor and Public Member Functions Circleldouble a, string n, double r): constructor that should call the base class constructor to set value of member area with a and name with n. The constructor will also set value of member radius with γ caledread: Overridden function that calculates the area of the circle (area = 3.14159 * radius * radius) and stores the result in the inherited member area. · print0: Overridden function that will print the radius, inherited member area and inherited member name. This function should use the base classs print function to print the area. 3. After you have created these classes, create a test program (Reference: Fig 12.17) Write a function named poly whose only parameter is a BasicShape pointer Function poly should use the BasicShape pointer to invoke calcArea function and print function. » In main0: define a Circle object with radius 10, name Round and area initialized to 0 Frommain), call the function poly such that it will polymorphically invoke calcArea function and print function of the Circle object.

My code:

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

class BasicShape { //Abstract base class

protected:

double area;

private:
   string name;

public:

BasicShape(double a, string n) {
area=a;
name=n;
}

void virtual calcArea()=0;//Pure Virtual Function

virtual void print() {

cout<<"Area of "<<getName()<<" is: "<<area<<"\n";

}
string getName(){
   return name;
}
};

class Circle : public BasicShape {

private:

double radius;

public:

Circle(double a, string n, double r): BasicShape(a,n) {

radius=r;

}

void calcArea() {

area = 3.14159*radius*radius; //use include math for pi and pow

}

void print() {

cout<<"Radius is: "<<radius<<" and area is "<<BasicShape::print()<<"of "<<getName()<<"\n";
}

};

void poly(BasicShape *bs) {

bs->calcArea();

bs->print();

}

int main() {

Circle c(0,"Round", 10);
poly(&c);
cout<<"\n\n";
return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
class BasicShape
{
//a) Protected Member Variable: area (a double used to hold the shape’s area).
protected:
double area;
//b) Private Member Variable: name (a string to indicate the shape’s type)
private:
string name;
public:
//BasicShape(double a, string n): A constructor that sets value of member area with a and member name with n.
BasicShape(double a, string n)
{
area = a;
name = n;
}
//calcArea(): This public function should be a pure virtual function.
virtual void calcArea() = 0;
//print(): A public virtual function that only prints the value of data member area.
virtual void print()
{
cout<<"Area: "<<area<<endl;
}
//getName():A public function that returns the value of data member name.
string getName()
{
return name;
}
};

answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
C++ Could you check my code, it work but professor said that there are some mistakes(check...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

  • I have given you a piece of code to test that your circle class works correctly....

    I have given you a piece of code to test that your circle class works correctly. Add your circle class to the code. public class Lab2Num1 { public static class Circle { private double radius; //your code goes here    //provide default constructor, constructor with one parameter, area, and circumference    } public static void main(String[] args) { Circle c = new Circle(1.5); System.out.printf("The circumference of a circle of radius " + c.getRadius()+ " is %5.2f\n", c.circumference()); System.out.printf("The area of...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  •       C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for...

          C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for this part Add on to the lab12a solution(THIS IS PROVIDED BELOW). You will add an overload operator function to the operator << and a sort function (in functions.h and functions.cpp)    Print out the customer’s name, address, account number and balance using whatever formatting you would like    Sort on account balance (hint: you can overload the < operator and use sort from the...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • solve it in c++ 10 note: please do not give me same answer like this 1....

    solve it in c++ 10 note: please do not give me same answer like this 1. Define a Pet class that stores the pet's name, age, and weight. Add appropriate constructors, accessor functions, and mutator functions. Also define a function named getLifespan that returns a string with the value "unknown lifespan." Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT