Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an instance of a class within a class (C++)

Tags:

c++

class

Let's say I have two classes: Box, Circle.

class Box{
int x, y;
...Box(int xcoord, int ycoord){printf("I'm a box."); x = xcoord; y = ycoord;}
};

class Circle{
...Circle(){printf("I'm a circle.");}
};

But let's say in the Circle class, I want to make an instance of the Box class. Well I tried this:

class Circle{
Box b(0,0);
...Circle(){printf("I'm a circle.");}
};

I get an error:

error C2059: syntax error : 'constant'

like image 540
Init Avatar asked Dec 21 '22 12:12

Init


1 Answers

class Circle {
    Box b;

public:
    Circle() : b(0, 0)
    {
        printf("I'm a circle.");
    }
};
like image 150
Fred Larson Avatar answered Jan 05 '23 03:01

Fred Larson