-
kill me
m gona kill myself if i dont find a tutorial that actually explains itself. well ill get to the point. this code...
Code:
class Dog {
public:
void setAge(int age);
int getAge();
void setWeight(int weight);
int getWeight();
void speak();
private:
int age;
int weight;
};
void Dog::setAge(int age)
{
this->age = age;
}
int Dog::getAge()
{
return age;
}
void Dog::setWeight(int weight)
{
this->weight = weight;
}
int Dog::getWeight()
{
return weight;
}
void Dog::speak()
{
cout << "BARK!!" << endl;
}
not explained at all, and this is the best tutorial that supposedly exists so can sum1 explain, line by line wat everythin means, i know its a big ask but im sure u can find it in your hearts :D :D :D :D :D :D :D :D
sorry, got carried away
-
I think I know why this tutorial is giving you some heartburn.. it does not provide you a sample driver program to let you see how everything works..
all you are seeing right now essentially is a class and function implementation.
If you want.. I can provide you a sample driver program that can tie all of this together.. and give you a chance to see how a complete program is supposed to work. :)
hang in there son.
-
yea, chuk that implementation prog my way
-
roger.. allow me like 20 mins to come up with something
-
The program is a tutorial on a simple class. You can break the sample code up into two parts.
First is the class defintion:
Code:
class Dog {
public:
void setAge(int age);
int getAge();
void setWeight(int weight);
int getWeight();
void speak();
private:
int age;
int weight;
};
Next we have the implementation:
Code:
void Dog::setAge(int age)
{
this->age = age;
}
int Dog::getAge()
{
return age;
}
void Dog::setWeight(int weight)
{
this->weight = weight;
}
int Dog::getWeight()
{
return weight;
}
void Dog::speak()
{
cout << "BARK!!" << endl;
}
The first part describes what functions make up the class, and what variables make up the class. For now, dont worry about the public/private parts.
The next part shows us the implementations of the functions that make up the class.
Here is a sample which shows us how to use that class:
Code:
int main()
{
Dog dog1;
Dog dog2;
dog1.setAge(3);
dog1.setWeight(50);
dog2.setAge(5);
dog2.setWeight(60);
cout << "Dog 1 is " << dog1.getAge() << " years old." << endl;
cout << "Dog 2 is " << dog2.getAge() << " years old." << endl;
return 0;
}
-
hey da brain wats takin u so long, i gotta get off in a min
-
Here is the tutorial.. complete with a driver program. :)
One of the joy's of object oriented programming.. as you have just learned first hand.. is code reuse.. you can write any driver program you want.. but still be able to use the same class and functions if you wanted to :)
Code:
#include<iostream>
using namespace std;
//Here is your class
class Dog {
public:
void setAge(int age);
int getAge();
void setWeight(int weight);
int getWeight();
void speak();
private:
int age;
int weight;
};
//Here is a driver program I just whipped up
int main()
{
//Create a Dog class object to allow access to class member functions
Dog pet;
int input;
cout << "\n\nEnter your dog's age: ";
cin >> input;
pet.setAge(input);
cout << "\nEnter your dog's weight (in pounds): ";
cin >> input;
pet.setWeight(input);
cout << "\n\n\n\nYour dog is " << pet.getAge()
<< " and weighs a hefty "<< pet.getWeight() << "lbs!!! ";
cout << "\n\nMaybe you should put your dog on a diet!";
cout << endl << endl << endl;
pet.speak();
pet.speak();
pet.speak();
return 0;
}
//Here is your FUNCTION IMPLEMENTATION
void Dog::setAge(int age)
{
this->age = age;
}
int Dog::getAge()
{
return age;
}
void Dog::setWeight(int weight)
{
this->weight = weight;
}
int Dog::getWeight()
{
return weight;
}
void Dog::speak()
{
cout << "BARK!!" << endl;
}
This code will compile. I'm not going to comment a lot on this program.. i think you'll be able to follow along with what's going on here now. :)
In fact.. I would like to see you write your own driver program :cool:
-
bithub's example is perfect.. it shows you you you can split up your program into 3 main parts...
[header file]
[driver program]
[function implementation]
once you have studied, "seperate compilation".. you'll see how you can save the header, driver, and implementation into their own individual files.. compile them into obj. (object files), and link them together to create a single .exe
it's easy. :cool:
-
*sniff sniff* what's that smell? Oh wait its another C++C_Forever. This dude doesn't even know what a function returning a value means but yet he is trying to learn classes.
-
-
check out this thread for some links to c++ tuts and a free book.
-
hmm if your so into learning C++ and arent happy with the tutorial you have, try buying a C++ book!!!!
i would recommend Accelerated C++ or thinking in C++
also the sams primer plus series are pretty good.
-
thankz, that free book saved me!