Click here to Skip to main content
15,504,146 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
my project talk about Shape hierarchy for every shape needs Inheritance, Polymorphism and Files I/0

I did a simple thing of the requirements, but I did not do it 100%, such as requirements 1, 2, 3 or requirements 6 and 7, so I sent all the requirements, please help me

Requirements
1. For every class, you need to build a default/parametrized constructor,
2. The default constructors set the objects' data members to 1.0,
3. Parametrized constructors are used to set objects' data members using the values passed to the objects during instantiation
4. readData() function is used to read all the object's data members from the user
5. print() function is used to print all object's data to the screen,
6. Your program must be able to write all objects' data to the binary file "Shapes.dat", using the printToFile functions,
7. Your program should be able to read and append to the "Shapes.dat" file as needed, using the readFromFile functions,
8. Your main file must be able to store/retrieve data to/from output/input files depending on the user's choice,
9. It is not known how many objects will be added or their order of addition.

What I have tried:

//Shapes.h


#pragma once

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

class Shape {
public:
    string color, name;
    Shape()
    {
        cout << "Base Class is Shape for this element . \n";
    }
};


//Shape2D.h

#pragma once

#include"shapes.h"
    class Shape2D : public Shape {
    public:

        double area, perimeter;
        Shape2D()
        {
            cout << "Parent Class is Shape2D for this element .\n";
        }
    };
//Shape3D.h

#pragma once

 #include"shapes.h"
    class Shape3D : public Shape {
    public:

        Shape3D()
        {
            cout << "Parent Class is Shape3D for this element .\n";
        }
    };
//Square.h

#include"shapes2D.h"
    class Square : public Shape2D {

    public:
        Square()
        {
            cout << "Calculating square area and perimeter \n";
        }
        void computeArea(double side)
        {
            double area;
            area = side * side;
            print("Area", area);
            printToFile("Area", area);
        }
        void computePerimeter(double side) {
            double perimeter;
            perimeter = 4 * side;
            print("Perimeter", perimeter);
            printToFile("Perimeter", perimeter);
        }
        void print(string x, double y)
        {
            cout << x << " of Square = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Square = " << y << "\n";

        }
    };
//Triangle.h


#include"shapes2D.h"
    class Triangle : public Shape2D {

    public:
        Triangle()
        {
            cout << "This is a Triangle\n";
        }
        void computeArea(double base, double height) {
            double area;
            area = (height * base) / 2;
            print("Area", area);
            printToFile("Area", area);
        }
        void computePerimeter(double base, double height) {}
        void print(string x, double y)
        {
            cout << x << " of Triangle = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Triangle = " << y << "\n";

        }
    };
//Sphere.h

#include"shapes3D.h"

    class Sphere : public Shape3D {

    public:
        Sphere()
        {
            cout << "This is a Sphere\n";
        }
        void computeSurfaceArea(double radius) {
            double area;
            area = 4 * 3.14 * radius * radius;
            print("Area", area);
            printToFile("Area", area);
        }
        void computeVolume(double radius) {
            double perimeter;
            perimeter = (4 / 3) * (3.14 * radius * radius * radius);
            print("Perimeter", perimeter);
            printToFile("Perimeter", perimeter);
        }
        void print(string x, double y)
        {
            cout << x << " of Sphere = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Sphere = " << y << "\n";

        }
    };
//Hexagon.h

#include"shapes2D.h"
    class Hexagon : public Shape2D {

    public:
        Hexagon()
        {
            cout << "This is a Hexagon\n";
        }
        void computeArea(double side) {
            double area;
            area = 3 * sqrt(3) * side * side / 2;
            print("Area", area);
            printToFile("Area", area);
        }
        void computePerimeter(double side) {
            double perimeter;
            perimeter = 6 * side;
            print("Perimeter", perimeter);
            printToFile("Perimeter", perimeter);
        }
        void print(string x, double y)
        {
            cout << x << " of Hexagon = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Hexagon = " << y << "\n";

        }
    };
//Cylinder.h

#include"shapes3D.h"

    class Cylinder : public Shape3D {

    public:
        Cylinder()
        {
            cout << "This is a Cylinder\n";
        }
        void computeSurfaceArea(double radius, double height) {
            double area;
            area = (2 * 3.14 * radius) * (radius + height);
            print("Surface Area", area);
            printToFile("Surface Area", area);
        }
        void computeVolume(double radius, double height) {
            double volume;
            volume = 3.14 * radius * radius * height;
            print("Volume", volume);
            printToFile("Volume", volume);
        }
        void print(string x, double y)
        {
            cout << x << " of Cylinder = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Cylinder = " << y << "\n";

        }
    };
//Cube.h

#include"shapes3D.h"
    class Cube : public Shape3D {

    public:
        Cube()
        {
            cout << "This is a Cube\n";
        }
        void computeSurfaceArea(double side) {
            double area;
            area = 6 * side * side;
            print("Surface Area", area);
            printToFile("Surface Area", area);
        }
        void computeVolume(double side) {
            double volume;
            volume = side * side * side;
            print("Volume", volume);
            printToFile("Volume", volume);
        }
        void print(string x, double y)
        {
            cout << x << " of Cube = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Cube = " << y << "\n";

        }
    };
//Cone.h

#include"shapes3D.h"
#include<cmath>
    class Cone : public Shape3D {
        double radius, height;
    public:
        Cone()
        {
            cout << "This is a Cone\n";
        }
        void computeSurfaceArea(double radius, double height) {
            double area;
            area = 3.14 * radius * (radius + sqrt(radius * radius + height * height));
            print("Surface Area", area);
            printToFile("Surface Area", area);
        }
        void computeVolume(double radius, double height) {
            double volume;
            volume = (3.14 * radius * radius * height) / 3;
            print("Volume", volume);
            printToFile("Volume", volume);
        }
        void print(string x, double y)
        {
            cout << x << " of Cone = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Cone = " << y << "\n";

        }
    };
//Circle.h


#include"shapes2D.h"
    class Circle : public Shape2D{
        double radius;
    public:
        Circle()
        {
            cout << "Calculating circle area and perimeter \n";
        }
        void computeArea(double r)
        {
            double area;
            area = 3.14 * r * r;
            print("Area", area);
            printToFile("Area", area);

        }
        void computePerimeter(double r) {
            double perimeter;
            perimeter = 2 * 3.14 * r;
            print("Perimeter", perimeter);
            printToFile("Perimeter", perimeter);

        }
        void print(string x, double y)
        {
            cout << x << " of Square = " << y << "\n";
        }
        void printToFile(string x, double y)
        {
            ofstream ofs;
            ofs.open("shapes.dat");
            if (!ofs) {
                cout << "Error opening file" << endl;

            }
            cout << "Shapes.dat file updated .";
            ofs << x << " of Circle = " << y << "\n";

        }

    };
//main.cpp


#include"Cone.h"
#include"Cube.h"
#include"Cylinder.h"
#include"Hexagon.h"
#include"sphere.h"
#include"square.h"
#include"Triangle.h"
#include "Circle.h"

int main()
{
    Cylinder a;
    Cube b;
    Cone c;



    double r, side, base, height;
    int n;
    cout << "Select any Number to calculate : ";
    cout << "1. Circle \n";
    cout << "2. Square\n";
    cout << "3. Triangle\n";
    cout << "4. Hexagon\n";
    cout << "5. Sphere\n";
    cout << "6. Cube\n";
    cout << "7. Cylinder\n";
    cout << "8. Cone\n";
    cin >> n;

    if (n == 1) {
        Circle obj1;
        cout << "Circle Radius";
        cin >> r;
        obj1.computeArea(r);
        obj1.computePerimeter(r);
    }

    if (n == 2) {
        Square obj2;
        cout << "Square Side";
        cin >> side;
        obj2.computeArea(side);
        obj2.computePerimeter(side);
    }

    if (n == 3) {
        Triangle obj3;
        cout << "Triangle Base";
        cin >> base;
        cout << "Triangle Height";
        cin >> height;
        obj3.computeArea(base, height);
        // obj3.computePerimeter(base,height) ;
    }

    if (n == 4) {
        Hexagon obj4;
        cout << "Hexagon Side";
        cin >> side;
        obj4.computeArea(side);
        obj4.computePerimeter(side);
    }

    if (n == 5) {
        Sphere obj5;
        cout << "Sphere Radius";
        cin >> r;
        obj5.computeSurfaceArea(r);
        obj5.computeVolume(r);
    }

    if (n == 6) {
        Cube obj6;
        cout << "Cube Side";
        cin >> side;
        obj6.computeSurfaceArea(side);
        obj6.computeVolume(side);
    }

    if (n == 7) {
        Cylinder obj7;
        cout << "Cylinder Radius";
        cin >> r;
        cout << "Cylinder Height";
        cin >> height;
        obj7.computeSurfaceArea(r, height);
        obj7.computeVolume(r, height);
    }

    if (n == 8) {
        Cone obj8;
        cout << "Cone Radius";
        cin >> r;
        cout << "Cone Height";
        cin >> height;
        obj8.computeSurfaceArea(r, height);
        obj8.computeVolume(r, height);
    }

    return 0;
}
Posted
Updated 8-May-22 12:25pm
v5
Comments
OriginalGriff 8-May-22 16:56pm    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Rick York 8-May-22 17:23pm    
One tip : consider making the Shape have virtual methods that the derived class all override or provide implementations for in the case of a pure virtual method. The Shape method would be the primary interface to users of the class(es).
Rick York 8-May-22 17:29pm    
Another tip : see the chain if statements in your main function? Add a method in every class to perform those operations. You could call it PromptAndCompute or something like that. Then you code looks like this :
if( n == 5 )
{
    Sphere obj;
    obj.PromptAndCompute();
}
Rick York 8-May-22 17:35pm    
If you want get fancy with it, you can implement an object factory as a static method of the Shape class. It's purpose would be to create objects of the specified type. That would eliminate all of those ifs in the user code and move it into the factory and they would appear in only one place - where the object is created. Using that would look like this :
cin >> index;
Shape * pobj = Shape::CreateShapeObject( int index );
pobj->PromptAndCompute();
delete pobj;
With virtual methods for all aspects of your requirements, the code to use them could be this simple.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900