-2

I have tried to run the program with more than one inheritance i got many errors For this program ,but when i use it for Source File, it will looks good

I don't know why but I think because i update my version or does make any sense

please help me with my project

int main

#include"Shapes.h"
#include"Cylinder.h"
#include"Sphere.h"
#include"Triangle.h"
#include"Square.h"

#define MAX_SHAPES 100

int main() {
    ofstream myfile;
    myfile.open("Shapes.dat");
    Shape* shapes[MAX_SHAPES];
    int currentShapes = 0;

    int n = 0;

    cout << "Select any Number to calculate : \n";
    cout << "1. Square\n";
    cout << "2. Triangle\n";
    cout << "3. Sphere\n";
    cout << "4. Cylinder\n";
    cout << "Enter your choice: ";
    cin >> n;

    cin.ignore();

    if (n == 1) {
        Square* obj2 = new Square();
        obj2->readData();
        shapes[currentShapes] = obj2;
    }

    if (n == 2) {
        Triangle* obj3 = new Triangle();
        obj3->readData();
        shapes[currentShapes] = obj3;
    }


    if (n == 3) {
        Sphere* obj5 = new Sphere();
        obj5->readData();
        shapes[currentShapes] = obj5;
    }


    if (n == 4) {
        Cylinder* obj7 = new  Cylinder();
        obj7->readData();
        obj7->computeSurfaceArea();
        obj7->computeVolume();
        obj7->print();
        obj7->printToFile();
        shapes[currentShapes] = obj7;

    }

    return 0;
}

shape.h

class Shape {
private:
    string color, name;
public:
    // default constructor
    Shape()
    {
        color = 1.0;
        name = "";
        cout << "Base Class is Shape for this element . \n";
    }
    // parameterized constructor
    Shape(string color, string name) {
        this->color = color;
        this->name = name;
    }
    // read data
    void readData() {
        cout << "Enter color: ";
        getline(cin, color);
        cout << "Enter name: ";
        getline(cin, name);
    }
    virtual void print() = 0;
    virtual void printToFile() = 0;
    virtual void readFromFile(ifstream& input) = 0;

};

shape2d.h

class Shape2D : public Shape {
public:
    double area;
    double perimeter;
    Shape2D() :Shape()
    {
        cout << "Parent Class is Shape2D for this element .\n";
    }
    // parameterized constructor
    Shape2D(string color, string name) :Shape(color, name) {
    }
    // read data
    void readData() {
        Shape::readData();
    }
    virtual void computeArea() = 0;
    virtual void computePerimeter() = 0;
    virtual void print() = 0;
    virtual void printToFile() = 0;
    virtual void readFromFile(ifstream& input) = 0;

};

Shape3d

class Shape3D : public Shape {
public:
    double surfaceArea;
    double volume;
    Shape3D() :Shape()
    {
        surfaceArea = 1.0;
        volume = 1.0;
        cout << "Parent Class is Shape3D for this element .\n";
    }
    // parameterized constructor
    Shape3D(string color, string name) :Shape(color, name) {
    }
    void readData() {
        Shape::readData();
    }
    virtual void computeSurfaceArea() = 0;
    virtual void computeVolume() = 0;
    virtual void print() = 0;
    virtual void printToFile() = 0;
    virtual void readFromFile(ifstream& input) = 0;
};

Square.h

class Square : public Shape2D {
private:
    double side;
public:
    Square() :Shape2D()
    {
        side = 1.0;
        cout << "Calculating square area and perimeter \n";
    }
    // parameterized constructor
    Square(string color, string name, double side) :Shape2D(color, name) {
        this->side = side;
    }
    void readData() {
        Shape2D::readData();
        cout << "Square Side: ";
        cin >> side;
        cin.ignore();
        computeArea();
        computePerimeter();
    }
    void computeArea()
    {
        area = side * side;
    }
    void computePerimeter() {
        perimeter = 4 * side;
    }
    void print(string x, double y)
    {
        cout << x << " of Square = " << y << "\n";
    }
    void print() {
        print("Area: ", area);
        print("Perimeter", perimeter);
    }
    void printToFile(string x, double y)
    {
        ofstream ofs;
        ofs.open("shapes.dat", ios_base::app);
        if (!ofs) {
            cout << "Error opening file" << endl;

        }
        cout << "Shapes.dat file updated .";
        ofs << x << " of Square = " << y << "\n";
        ofs.close();
    }
    void printToFile() {
        printToFile("Area: ", area);
        printToFile("Perimeter", perimeter);
    }
    void readFromFile(ifstream& input)
    {
        string data = "";
        while (std::getline(input, data))
        {
            std::cout << data << endl;
        }
    }
};

Triangle.h

class Triangle : public Shape2D {
private:
    double base;
    double height;
public:
    Triangle() :Shape2D()
    {
        cout << "This is a Triangle\n";
    }
    // parameterized constructor
    Triangle(string color, string name, double base, double height) :Shape2D(color, name) {
        this->base = base;
        this->height = height;
    }
    void readData() {
        Shape2D::readData();
        cout << "Triangle Base: ";
        cin >> base;
        cout << "Triangle Height: ";
        cin >> height;
        cin.ignore();
        computeArea();
        computePerimeter();
    }
    void computeArea() {
        area = (height * base) / 2;
    }
    void computePerimeter() {
        perimeter = 2 * height + base;
    }
    void print(string x, double y)
    {
        cout << x << " of Triangle = " << y << "\n";
    }
    void print() {
        print("Area: ", area);
        print("Perimeter: ", perimeter);
    }
    void printToFile(string x, double y)
    {
        ofstream ofs;
        ofs.open("shapes.dat", ios_base::app);
        if (!ofs) {
            cout << "Error opening file" << endl;

        }
        cout << "Shapes.dat file updated .";
        ofs << x << " of Triangle = " << y << "\n";
        ofs.close();
    }
    void printToFile() {
        printToFile("Area: ", area);
        printToFile("Perimeter", perimeter);
    }
    void readFromFile(ifstream& input)
    {
        string data = "";
        while (std::getline(input, data))
        {
            std::cout << data << endl;
        }
    }
};

Sphere.h

class Sphere : public Shape3D {
private:
    double radius;
public:
    Sphere() :Shape3D()
    {
        radius = 1.0;
        cout << "This is a Sphere\n";
    }
    // parameterized constructor
    Sphere(string color, string name, double side) :Shape3D(color, name) {
        this->radius = radius;
    }
    void readData() {
        Shape3D::readData();
        cout << "Sphere Radius: ";
        cin >> radius;
        cin.ignore();
        computeSurfaceArea();
        computeVolume();
    }
    void computeSurfaceArea() {
        surfaceArea = 4 * 3.14 * radius * radius;
    }
    void computeVolume() {
        volume = (4 / 3) * (3.14 * radius * radius * radius);
    }
    void print(string x, double y)
    {
        cout << x << " of Sphere = " << y << "\n";
    }
    void print() {
        print("Surface Area: ", surfaceArea);
        print("Volume: ", volume);
    }
    void printToFile(string x, double y)
    {
        ofstream ofs;
        ofs.open("shapes.dat", ios_base::app);
        if (!ofs) {
            cout << "Error opening file" << endl;

        }
        cout << "Shapes.dat file updated .";
        ofs << x << " of Sphere = " << y << "\n";
        ofs.close();
    }
    void printToFile() {
        printToFile("Surface Area: ", surfaceArea);
        printToFile("Volume", volume);
    }
    void readFromFile(ifstream& input)
    {
        string data = "";
        while (std::getline(input, data))
        {
            std::cout << data << endl;
        }
    }
};

Cylinder .h

class Cylinder : public Shape3D {
private:
    double radius;
    double height;
public:
    Cylinder() :Shape3D()
    {
        radius = 1.0;
        height = 1.0;
        cout << "This is a Cylinder\n";
    }
    // parameterized constructor
    Cylinder(string color, string name, double radius, double height) :Shape3D(color, name) {
        this->radius = radius;
        this->height = height;
    }
    void readData() {
        Shape3D::readData();
        cout << "Cylinder Radius: ";
        cin >> radius;
        cout << "Cylinder Height: ";
        cin >> height;
        cin.ignore();

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

        }
        cout << "Shapes.dat file updated .";
        ofs << x << " of Cylinder = " << y << "\n";
        ofs.close();
    }
    void printToFile() {
        printToFile("Surface Area: ", surfaceArea);
        printToFile("Volume", volume);
    }
    void readFromFile(ifstream& input)
    {
        string data = "";
        while (std::getline(input, data))
        {
            std::cout << data << endl;
        }
    }
};
Jason
  • 36,170
  • 5
  • 26
  • 60
  • 2
    First of all don't forget the ***minimal*** part of your [mre]. Secondly, please copy-paste (as text!) the full and complete build log into the question. – Some programmer dude May 13 '22 at 08:34
  • None of your headers appear to include any other files. I see `#include"Shapes.h"` but no file with that name, and nothing appears to include `shape2d.h` or `shape3d.h` All perfectly good reasons for this code to fail to compile not even looking at the code itself. – Retired Ninja May 13 '22 at 08:37

1 Answers1

1

In shape2d.h and shape3d.h with have to add

#include "shape.h"

In square.h and triangle.h with have to add

#include "shape2d.h"

In sphere.h and cylinder.h you have to add

#include "shape3d.h"

And to avoid multiple inclusions add at the beginning of each .h file

#pragma once

or if your compiler doesn't support it enclose the hole code inside any .h in something like

#ifndef YOUR_H_FILE_NAME_H
#define YOUR_H_FILE_NAME_H
// ... your .h code here
#endif

Further in main.cpp you include shapes.h which doesn't exist but you can remove it because you already include all the specilized .h file

Marco Beninca
  • 605
  • 4
  • 15
  • Don't create any kind of symbols using double leading underscore yourself, they are reserved. See [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Some programmer dude May 13 '22 at 09:07
  • @Someprogrammerdude you are right... I edit the post to avoid it – Marco Beninca May 13 '22 at 09:09