LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2005, 03:24 AM   #1
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Rep: Reputation: 15
Smile Help On C++ Classes, PLS.


Hi again everyone,

Thanks for your help on functions it was very informitive.

I am now learning classes... well trying to learn. It is pretty confusing I guess it might just take a while to learn them. Does anyone here know of any really helpful tutorials, advice or just something that will help me learn them in an easier way?

I am currently learning from SAMS 24 Hour C++ guide.

Thanks for your help
 
Old 08-21-2005, 03:47 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I've heard good things about Computing Concepts with C++ Essentials, 3rd Edition [ISBN 0-471-16437-2] (although I've only read the Java version, which was very good).

If you are looking for online tutorials, then you probably want to be Googling for “encapsulation” and/or “object-orientation” as well as “class” and “object”.

You'll probably find there's a lot of information on classes as an abstract concept as well as C++-specific code.
 
Old 08-21-2005, 05:38 AM   #3
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
I have been trying to understand a program from SAMS 24 Hour C++ book.
Code:
#include <iostream>

using namespace std;

class Cat{
public:
int GetAge();
void SetAge(int age);
void Meow();
private:
int itsAge;
};
//GetAge, Public accessor function
//returns value of itsAge member
int Cat::GetAge()
{
return itsAge;
}

//definition of SetAge, public
//accessor function.
//sets itsAge member.
void Cat::SetAge(int age)
{
//set member variable its age to
//value passed in by parameter age
itsAge = age;
}

//definition of Meow method
//returns: void
//Parameters: none
//Action: Prints "meow" to screen
void Cat::Meow()
{
cout<<"Meow.\n";
}


//Create a cat, set its age, have it
//meow, tell us its age, then meow again.
int main()
{
Cat Frisky;

Frisky.SetAge(5);
Frisky.Meow();
cout<<"Frisky is a cat who is ";
cout<<Frisky.GetAge()<<" years old.\n";
Frisky.Meow();

cin.get();
cin.get();
return 0;
}
Some things I don't understand:
  • In the book he says under Classes Versus Objects,

    "You never pet the definition of a cat; you pet individual cats. You draw a distinction between the idea of a cat and the particular cat that is right now shedding all over your living room. In the same way, C++ differentiates between the class Cat, which is the idea of a cat, and each individual Cat object. Thus, Frisky is an object of type Cat in the same way in which GrossWeight is a variable of type unsigned int.

    An object is simply an individual instance of a class."
  • Are the functions within a class (methods) are also prototypes or do they serve the same purpose of a prototype?
  • Also when initialize the member data of a class do you have to use a constructor or can you just write for example:
    Code:
    class Cat{
    int Weight = 10;
    };
 
Old 08-21-2005, 05:45 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally posted by InvisibleSniper
Some things I don't understand:
  • In the book he says under Classes Versus Objects,

    "You never pet the definition of a cat; you pet individual cats. You draw a distinction between the idea of a cat and the particular cat that is right now shedding all over your living room. In the same way, C++ differentiates between the class Cat, which is the idea of a cat, and each individual Cat object. Thus, Frisky is an object of type Cat in the same way in which GrossWeight is a variable of type unsigned int.

    An object is simply an individual instance of a class."
What aren't you understanding about this bit?

Quote:
  • Are the functions within a class (methods) are also prototypes or do they serve the same purpose of a prototype?
Yeah, if you have something like

Code:
class SomeClass
{
       public:
           void myFunction();
};
, that is effectively a prototype. Remember that you have to write the implementation for the function, eg.

Code:
SomeClass::myFunction()
{
     // do stuff
}
You can combine these into one step, as well.

Quote:
  • Also when initialize the member data of a class do you have to use a constructor or can you just write for example:
    Code:
    class Cat{
    int Weight = 10;
    };
I think you can probably do that, yeah. Try it, see if it'll compile .

Last edited by Nylex; 08-21-2005 at 05:46 AM.
 
Old 08-21-2005, 06:13 AM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Are the functions within a class (methods) are also prototypes or do they serve the same purpose of a prototype?
A method is just a series of instructions that happens to be attached to a class. You call a method on an object, not on a class; the method might change the state of the object but it can't change the definition of the class (which is a blueprint or prototype for future objects and so doesn't change). (The exception to this are static objects, but even these can't change the class).

Quote:
Also when initialize the member data of a class do you have to use a constructor or can you just write for example:
Code:
class Cat{
int Weight = 10;
};
Leaving aside the fact that there's no accessor or mutator methods that can get or set the Weight variable, this is a bad idea.

Constructors are good; they initialise the data in one well-defined place. When you start getting complex objects, initialising them in the class template would become unwieldy. You can define static const variables like that (except that you have to put the definition outside the class declaration), but these are special because they never change during the run of a program.

One thing I've learnt about C++ is not to look for short-cuts. There are many ways you can type a program in faster (not declaring copy constructors, not overloading += when you overload +, and so on), but — without exception — each and every short-cut you take causes serious head-aches when you're trying to debug the program.
 
Old 08-21-2005, 07:25 AM   #6
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
I made my own class called car and two objects, one called Ferrari and one called FourWheel Drive. What I wanted my program to do was to use the function "GetSpeed()" to accept user's input then to print a string on the screen in relation to how it would sound if you were going the speed the user entered. There is 20 errors in my program, I don't know what they are, I tried fixing it. If someone could help again it would be great thank you
Code:
#include <iostream>

using namespace std;

class Car{
public:
void GetSpeed();
int MaxSpeed;
};


Car Ferrari;  //Declare an object of Ferrari
Car FourWheelDrive; //Declare an object of 4WD.

Ferrari.MaxSpeed = 450;//This is the maximum speed of the ferrari anything
//faster will be an error .
FourWheelDrive.MaxSpeed = 130;  //This is the maximum speed of the 4WD anything
//faster will be an error .
unsigned int usersSpeed;

usersSpeed = Car::GetSpeed(usersSpeed);//Make sure the return value goes into "usersSpeed" variable.


cout<<"Please enter how fast your ferrari is going in Kilometers: ";
cin<<usersSpeed;
Car::GetSpeed();
cout<<"Please enter how fast your 4WD is going in Kilometers: ";
cin<<usersSpeed;
Car::GetSpeed();

//Define my GetSpeed() function.

void Car::GetSpeed(int usersSpeed){



if((usersSpeed == 0) || (usersSpeed <= 80))
{
cout<<"brm brm brm chug brm";
}
if ((usersSpeed > 80) && (usersSpeed <= 130))
{
cout<<"brrrrrrrrrrrrrrrrrrrrrm brrrrrrrrrrrrrrrrrrrrrr\nrrrrrrrrrrrrrrrrrrrm";
}
if ((usersSpeed > 130) && (usersSpeed < 450))
{
cout<< "brrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
<<"\nrrrrrm roooooooooooooooooom brrrrrrrrrrrrrrrrrrrrrrrm rrrrrrrrrrrrrbrm brr\n"
<<"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm roaw";
}
else
cout<<"Error, either you entered invalid data or your car just can't go that fast!";
}
 
Old 08-21-2005, 07:30 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
What are the errors you're getting? The first thing is that your program has no main() function!

Code:
usersSpeed = Car::GetSpeed(usersSpeed);
Firstly, when you put the function in your Car class declaration, you didn't put any parameters! Your function implementation has a parameter, but the prototype does not! Both of these need to be the same.

Secondly, you can't call GetSpeed() like this, you need to call it on an individual Car object (that's what the thing about petting individual cats was about), because it isn't defined as static. You'd have to use something like

Code:
usersSpeed = aCar.GetSpeed();
assuming of course you had already declared a Car object called aCar. It wouldn't make sense to define this particular method as static, because when you talk about the speed of a car, you're talking about a specific car. Static methods are for class methods - they belong to the class and not individual objects. One use of them would be to get the number of Car objects that had been created.

Last edited by Nylex; 08-21-2005 at 07:38 AM.
 
Old 08-21-2005, 07:45 AM   #8
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally posted by InvisibleSniper
Code:
#include <iostream>

using namespace std;

class Car{
public:
void GetSpeed();
int MaxSpeed;
};
First reaction is not positive. MaxSpeed is a public member variable. You should not use a public member variable. Edit: Ever.

Quote:
Code:
Car Ferrari;  //Declare an object of Ferrari
Car FourWheelDrive; //Declare an object of 4WD.
OK, but these are now global variables.

You should declare varaibles as close as possible to the point where they are used.

Quote:
Code:
Ferrari.MaxSpeed = 450;//This is the maximum speed of the ferrari anything
//faster will be an error .
FourWheelDrive.MaxSpeed = 130;  //This is the maximum speed of the 4WD anything
//faster will be an error .
These variables should be there from the moment the object's lifetime starts; i.e. as soon as the constructor returns. Pass the values as parameters to the constructor and use them to initialise the class members. Otherwise your program will be harder to maintain as you have to remember where the constructor starts.

Quote:
Code:
unsigned int usersSpeed;

usersSpeed = Car::GetSpeed(usersSpeed);//Make sure the return value goes into "usersSpeed" variable.
You could combine these into one statement, but there's not much in it.

Again, this should go inside the function where it's used.

Car::GetSpeed is not declared as a static function, so you can't do that.
Quote:
Code:
cout<<"Please enter how fast your ferrari is going in Kilometers: ";
Kilometers is a measure of distance, not speed.
You can't use operators like << (or methods, or functions) outside of a subroutine.
Quote:
Code:
cin<<usersSpeed;
std::basic_istream:perator << is not defined by the standard. Did you mean >>?
Quote:
Code:
Car::GetSpeed();
cout<<"Please enter how fast your 4WD is going in Kilometers: ";
cin<<usersSpeed;
Car::GetSpeed();
As above. Note that no value is being passed to GetSpeed(), and GetSpeed() is still not a static method.
Quote:
Code:
//Define my GetSpeed() function.

void Car::GetSpeed(int usersSpeed){



if((usersSpeed == 0) || (usersSpeed <= 80))
{
cout<<"brm brm brm chug brm";
}
if ((usersSpeed > 80) && (usersSpeed <= 130))
{
cout<<"brrrrrrrrrrrrrrrrrrrrrm brrrrrrrrrrrrrrrrrrrrrr\nrrrrrrrrrrrrrrrrrrrm";
}
if ((usersSpeed > 130) && (usersSpeed < 450))
{
cout<< "brrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
<<"\nrrrrrm roooooooooooooooooom brrrrrrrrrrrrrrrrrrrrrrrm rrrrrrrrrrrrrbrm brr\n"
<<"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm roaw";
}
else
cout<<"Error, either you entered invalid data or your car just can't go that fast!";
}
This method doesn't use MaxSpeed; you might want to consider making it static.

The prototype of this function doesn't match the declaration. You declared this as
Code:
void Car::GetSpeed();
but defined it as
Code:
void Car::GetSpeed(int usersSpeed) { … }
These are not equivalent; you need to put int usersSpeed in the declaration.

You also never pass a variable to the method.
Code:
Car::getSpeed(usersSpeed);
There is no relationship between the global variable usersSpeed that you defined earlier and the usersSpeed parameter variable in the function. Defining the latter will actually stop you from being able to access the former from within the class (but since you don't want to do this anyway, you shouldn't have made it global in the first place).
 
Old 08-21-2005, 08:32 AM   #9
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
What could I write that will help me learn classes? I mean what type of program... what would the program do?

Thanks For Your Help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 10:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 10:10 PM
classes in C++ niteshadw Programming 2 07-05-2005 07:05 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration