Hi,
I'm new here and pretty new to c++ and programming in general.
During one of my first excercises I tried to work with classes but I don't know how to output an class object in another class.

Here is my Code with the text in [] (Circle.cpp) where I'm stuck.
Would be great if anyone could help.
Thanks

Point.h

#pragma once
#include <iostream>
#include <string>

using namespace std;

class Point 
{
private:            

    int X;
    int Y;


public:             

    Point(); 
    Point(int, int); 
    ~Point(); 

    int getX();
    int getY();

    void setX(int);
    void setY(int);

    void Print();
};

Shapes.h (BASE)

#pragma once
#include <iostream>
#include <string>

using namespace std;

class Shapes
{
    protected:           

        string name;

    public:             

        Shapes(); 
        Shapes(string);
        ~Shapes(); 

        string getName();

        void setName(string);

        void Print();
    };

Circle.h

#pragma once
#include <iostream>
#include <string>
#include "Point.h"

using namespace std;

class Circle: public Shape
{
private:             

    Circle Middle;
    double Radius;


public:             

    Circle();  
    Circle(string); 
    ~Circle(); 

    void Print();
    void SetPointRadius(Circle,double);
};

Point.cpp

#include <iostream>
#include <string>
#include "Shapes.h"


using namespace std;

Point::Point() {
    X = 0;
    Y = 0;
}
Point::Point(int X_, int Y_) {
    X = X_;
    Y = Y_;
}
Point::~Point() {
}

int Point::getX() {
    return X;
}

int Point::getY() {
    return Y_;
}

void Point::setX(int X_) {
    X = X_;
}

void Point::setY(int Y_) {
    Y = Y_;
}

void Point::Print() {
    cout << "X: " << X << endl;
    cout << "Y: " << Y << endl;
}

Circle.cpp

#include <iostream>
#include <string>
#include "Shapes.h"
#include "Circle.h"


using namespace std;

Circle::Circle() {
    Radius = 0;
} 

Circle::Circle(string name_) {
    name = name_;
}

Circle::~Circle() {
}

void Circle::Print() {


        [ Output: Shapes::Print() ]     // void Shapes::Print() {cout << "Name is: " << name << endl;}

    cout << "Point: " << endl;  

    [ Output: Point::getX() ]       // int Point::getX() {return X;}
    [ Output: Point::getY() ]       // int Point::getY() {return Y;}

    cout << endl;
    cout << "Radius: " << Radius << endl;


}

void CKreis::SetPointRadius(CPunkt Mitte, double Radius) {
    PMitte = Mitte;
    myRadius = Radius;
}

Main.cpp

#include <iostream>
#include "Shapes.h"
#include "Circle.h"
#include "Point.h"

using namespace std;

int main() {

    Circle C ("Circle 1");
    Point P1(1, 3);
    double Radius = 2.5;
    P1.SetPointRadius(P1, Radius);

    P1.Print();
    cout << endl;
}

I can see a number of issues with the code as given.

  • You define a class Shapes but then declare Circle as a subclass of Shape. The names need to match in order to be valid.
  • You never #include the header Shapes.h in the Circle.h header.
  • You define Circle middle as an instance variable of Circle itself, which leads to a recursive definition. While one can have a recursive definition using a pointer (e.g., Circle* middle;), having an object of the type inside the class definition isn't valid.
  • Point::getX(), Point::getY(), and Point::Print() are all instance methods rather than class methods. There would need to be a Point object for them to be applied to. Applying an instance method is in the form myPoint.getX().

Presumably, what you want to do is have middle as a Point instead, and apply those methods to middle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.