Answer to Question #345795 in C++ for Dag

Question #345795

Take a real-world scenario of inheritance and implement it in C++ program. Your program should consist of the following: 

1. Use Constructors and appropriate access modifier 2. List and draw Base class and Derived class 3. Show polymorphism (function overriding and function overloading) 4. Multiple inheritance 5. Put the user’s data into a file and display a data from a file to the user. 

6. Implement Abstract class and interface


1
Expert's answer
2022-05-30T08:16:12-0400
#include <iostream>
#include <fstream>
using namespace std;
class SimpleShape{  // an abstract class
    protected:
        float width;    // width, height are useful for calculting how much 
        float height;   // free space a shape needs to be displayed correctly
    public:
    SimpleShape(float w, float h){
        width = w;
        height = h;
    }
    SimpleShape(int w, int h){
        width = float(w);
        height = float(h);
    }    
    virtual float getArea() = 0;  // the c++ implementation of interfaces - virtual functions


};
class Rectangle: public SimpleShape{
    
    public:
        Rectangle(float w, float h):SimpleShape(w,h){
        }
        Rectangle(int w, int h):SimpleShape(w,h){
        }    
        float getArea(){
            return width*height;
        }
};


class Triangle: public SimpleShape{


    public:
        Triangle(float w, float h):SimpleShape(w,h){
        }
        Triangle(int w, int h):SimpleShape(w,h){
        }     
        float getArea(){
            return 0.5*width*height;
        }
};
class Elipse: public SimpleShape{
    public:
        Elipse(float w, float h):SimpleShape(w,h){
        }        
        Elipse(int w, int h):SimpleShape(w,h){
        } 
        float getArea(){
            return 3.14*(width/2)*(height/2);
        }
};


class Square: public Rectangle{
    public:
        Square(float side):Rectangle(side, side){
        }
        Square(int side):Rectangle(side, side){
        }
        float getArea(){
        	return width*height;
		}
};


class Circle: public Elipse{
    public:
        Circle(float rad):Elipse(rad*2,rad*2){
        }
        Circle(int rad):Elipse(rad*2,rad*2){
        }        
        float getArea(){
        	return 3.14*width/2;
		}
};


int main() {
    Triangle(10,5);
    Circle c1(5);
    Square sq1(3);
    
    char path[] = "shapes.dat";
// write a Triangle to file for a test
    ofstream fileW;									// TODO: slice input/output to functions
    fileW.open(path, ios::app);			
    fileW.write( (char*)& sq1, sizeof(sq1) );
// read a Triangle from a file for a test
    ifstream fileR;
    fileR.open(path,ios::in);
    Square obj(0);
	fileR.read( (char*)& obj, sizeof(obj) );
    cout<<"[tets]] Area of the written-read Square: "<< obj.getArea();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS