Hi everyone, I am a noob in c++.
I have been studying ths topic called Objects and Classes
from the book: Introduction to Programming with C++ by Y. Daniel Liang
I started typing an example project from the book in my Dev C++ compiler.
I wrote everything exactly as printed in the book, but i just get a bunch
of errors that i don't even know what they mean.
I will post the files code then the Compiling Errors as last.
//This is the header file Circle.h
class Circle ( ) {
public:
// The radious of this circle
double radious;
// Here is construct a default cirlce object
Circle ( );
// Construct a circle object
Circle(double);
//Return the area of this circle
double getArea( );
};
// Circle.cpp this is file 2 from the Project
#include "Circle.h"
// Construct a default circle object
Circle::Circle ( ) {
radious = 1;
}
// Constructs a circle object
Circle::Circle ( doble newRadious ) {
radious = newRadious;
}
// Return the area of this circle
double Circle::getArea ( ) {
return raidious * radious * 3.14159;
}
// This last file will test the whole project TestCircle.cpp
#include <iostream>
#include "Circle.h"
usingnamespace std;
int main ( ) {
Circle circle1;
Circle circle2 ( 5.0 );
cout << " The are of the circle of radious " << circle1.radious << " is " << circle1.getArea ( ) << endl;
cout << " The are of the circle of radious " << circle2.radious << " is " << circle2.getArea ( ) << endl;
// Modify the circle radious
circle2.radious = 100;
cout << " The area of the circle of radious " << circle2.radious << " is " << circle2.getArea ( ) << endl;
return 0;
}
1) At line 4 in your OP: You shouldn't have the parentheses in that class definition. That's probably causing most of your compilation errors.
2) Line 31: That's not how you spell double.
3) Why are you making your data member public? Encapsulation is one of the fundamental principles of OOP; by making radious public, you're violating that.
4) Line 27: In your constructor, it's better to use an initialisation list to initialise data members, rather than assigning values in the body of the constructor. You may not have come across this yet in your learning.
5) In general, you've misspelled "radius". Not a huge problem in itself, however...
6) At line 39, you haven't spelled it the same way as in your class definition, and that will cause a compilation error.
Thank you MikeyBoy, I appreciate your help. I now why my mates tell me not to stay until morning studying. I didn't realize those parentheses at all nor the spelling errors. Class declaration should not have parentheses, only default objects or ther objectf of the same class or anonymous objects.