Originally Posted by
Shadowwoelf
1. Can you have multiple classes in one file? as in #ifndef...#endif then another #ifndef...
Yes. There is no rule preventing a header file from declaring multiple classes, a source file from implementing multiple classes.
In fact, there is technically no mandate that a project will use header files at all. Such things are a convention. Albeit a convention that is often applied.
In practice, people will tend to be able to maintain code better if the project is systematically broken into chunks (eg one header file that declares a class and one source file that implements its member functions). Which means it is often a good idea to do that to avoid driving team members crazy.
Originally Posted by
Shadowwoelf
2. does the file name have to be the same as the class name? aka circle.h and the file has to be circle?
Nope. There is technically nothing stopping a programmer from declaring a class named circle in a header file named "square.h" and implementing its member functions in a source file named "triangle.cpp". Except the impact that will have by driving other programmers crazy (or yourself if you have to maintain the code after a break of a couple of years).
In a professional setting, your pay or continuing employment prospects will often be linked to performance, and team leaders will tend to frown on such things being done for no good reason, and view them as poor performance. So, if you are going to do such things, make sure that it makes sense to the team.
If you are writing code for yourself, it is probably a good idea to be systematic about how you do things. After all, it does help if you can actually understand what you have done.
Originally Posted by
Shadowwoelf
3. Do classes only handle data? Can the member functions cout stuff to the screen and get input?
Member functions can do anything you like.
Originally Posted by
Shadowwoelf
4.What exactly does a constructor do? Like lets say you want to house a constructor for a house, would you have to set all the variables there to zero
To understand that, you need to understand that there are two phases in construction of an object. The first is getting raw memory (everything that contains data is raw memory as far as a computer is concerned) and the second is turning that raw memory into something that a human will call an "object".
A constructor is a special function that the compiler invokes to turn the raw memory into an "object". After the constructor has finished, the object will exist. Typically the constructor initialises data members of the class instance to some sensible values that make sense for that class.
Originally Posted by
Shadowwoelf
5. Can someone please explain to me what this "copy constructor" does because I have no clue on that
A copy constructor is a constructor that is invoked when creating a copy of an object that already exists. Let's say you have;
Code:
// appropriate declarations of circle class
int main()
{
circle a;
// set radius of a to be something
circle b(a)
}
then the circle b is a new object which is a copy of a (will have the same radius). If you subsequently change a, b does not change (i.e. they are different objects).
The copy constructor is invoked in the process of creating b. It needs to receive information about a to be able to copy it.
Originally Posted by
Shadowwoelf
Code:
public:
circle();
circle(const circle &);<-copy constructor
private:
....
circle::circle(const circle & Object)<-Dont know what object exactly does
{
radius=object.radius;
}
This (other than typos: Object should be object) is a copy constructor. The argument object, in my little example above, is a reference to a. The line "radius = object.radius;" effectively sets b.radius to be equal to a.radius.
Originally Posted by
Shadowwoelf
Tell me if you need the entire coding for the copy constructor but yea thanks for reading all my questions
Depends on what you mean by "entire coding". A copy constructor (like any constructor) should correctly initialise the object being created. Otherwise code which works on that object (including other member functions) cannot work correctly if they assume the object is correctly initialised.
Copy constructors are special though: if you don't provide one, the compiler will usually provide one for you. The default is a member-wise copy of the object being copied. If that default behaviour does not work correctly (and the notion of correctly is up to the programmer) then the programmer needs to explicitly supply one that copies the object correctly.
In your example, you would probably not need to supply a copy constructor as the compiler-generated version will work correctly.
A common example where it is necessary to hand-roll a copy constructor is if dynamic memory is managed.
Code:
class X
{
public:
X() : x(new int [10])
{
for (int i = 0; i < 10; ++i) x[i] = i;
};
~X() {delete [] x;};
private:
int *x;
}
With this class, the code;
Code:
int main()
{
X a;
X b(a);
}
exhibits undefined behaviour (a crash as the program exits is a common symptom) as the compiler-supplied copy constructor will make a.x and b.x point to the same memory, and that memory will get deleted twice. This can be fixed by explicitly coding a copy constructor
Code:
class X
{
public:
X() : x(new int [10])
{
for (int i = 0; i < 10; ++i) x[i] = i;
};
X(const X&a) : x(new int[10])
{
for (int i = 0; i < 10; ++i) x[i] = a.x[i];
};
~X() {delete [] x;};
private:
int *x;
}