Classes and objects
4 posters
RUSES :: Part 1 :: Programming
Page 1 of 1
Classes and objects
Could someone please explain how you use classes, i understand how you create classes, i just cant get my code to use them and am unsure where i define variables and functions of the class
pidgeonsauce- Posts : 1
Join date : 2012-03-16
Re: Classes and objects
In general:
Extra info
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cprogramming.com/tutorial/lesson12.html
- Code:
class Class_Name
{
private:
int var1;
string var2;
public:
Class_Name(); //constructor
void doSomething(int n); //random function
};
Class_Name::Class_Name()
{
var1 = 4;
var2 = "hi";
}
void Class_Name::doSomething(int n)
{
var1 += n;
}
Extra info
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cprogramming.com/tutorial/lesson12.html
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes and objects
Can be hard to explain, best way is to follow the examples like in the links Alex gave, and actually write them out yourself. When it finally clicks you can really start to see why C++ is so popular.
ElliottH- Admin
- Posts : 16
Join date : 2012-02-23
Re: Classes and objects
Alex's Tutorial
Let's say we have the cat below, surround by his 'abilities'. i.e. he can meow, hiss or bite another cat. He also has a name:
This can be modelled as
Defining the functions below:
Using this class:
Let's say we have the cat below, surround by his 'abilities'. i.e. he can meow, hiss or bite another cat. He also has a name:
This can be modelled as
- Code:
class Cat
{
private:
string Name;
public:
void Meow();
void Hiss();
void Bite(Cat catThatIsBitten);
void SetName(string name);
string GetName() const;
};
Defining the functions below:
- Code:
void Cat::Meow()
{
cout << "Meow!" << endl;
}
void Cat::Hiss()
{
cout << "Hiss!" << endl;
}
void Cat::Bite(Cat catThatIsBitten)
{
cout << catThatIsBitten.GetName() << " was bitten by " << this->GetName() << endl;
//i.e. Tibbles was bitten by Snowy
}
void Cat::SetName(string name)
{
this->Name = name;
}
string Cat::GetName() const
{
return this->Name;
}
Using this class:
- Code:
int main()
{
Cat Tibbles;
Tibbles.SetName("Tibbles");
Cat Snowy;
Snowy.SetName("Snowy");
Tibbles.Meow();
Snowy.Hiss();
Snowy.Bite(Tibbles);
cin.get();
return 0;
}
Last edited by Alex on Sat Mar 17, 2012 8:27 am; edited 1 time in total
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes and objects
Constructors and Destructors
In the above code, it's quite annoying calling two lines to set the cat's name:
Wouldn't it be easier to make that only take up one line of code? (yes)
This is where constructors come in handy. Constructors are a function called when the class is created.
A constructor's function name is the same name as the class. So since the class name is Cat, the constructor will be Cat.
Constructors can take arguments and they don't return a value.
Below I've modified the class to add a constructor which takes a string parameter, the string passed into the constructor will be the name of the cat.
Now we need to define the constructor:
So the main function can now be modified:
In the above code, it's quite annoying calling two lines to set the cat's name:
- Code:
Cat Snowy;
Snowy.SetName("Snowy");
Wouldn't it be easier to make that only take up one line of code? (yes)
This is where constructors come in handy. Constructors are a function called when the class is created.
A constructor's function name is the same name as the class. So since the class name is Cat, the constructor will be Cat.
Constructors can take arguments and they don't return a value.
Below I've modified the class to add a constructor which takes a string parameter, the string passed into the constructor will be the name of the cat.
- Code:
class Cat
{
private:
string Name;
public:
//constructor
Cat(string Name);
void Meow();
void Hiss();
void Bite(Cat catThatIsBitten);
void SetName(string name);
string GetName() const;
};
Now we need to define the constructor:
- Code:
Cat::Cat(string name)
{
this->Name = name;
}
So the main function can now be modified:
- Code:
int main()
{
Cat Tibbles("Tibbles");
Cat Snowy("Snowy");
Tibbles.Meow();
Snowy.Hiss();
Snowy.Bite(Tibbles);
cin.get();
return 0;
}
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes and objects
A destructor is the opposite of a constructor, (although you must manually call it) when you want to delete an object
Destructors can take no parameter and they cannot return a value
It is defined like the constructor except it starts with a squiggle ~
i.e.
Constructor = Cat()
Destructor = ~Cat()
Though the example I'm about to do is pointless, it will help explain a destructor.
Let's say when we want to delete a Cat object, we want to set its name to "" - a blank string
First we add the destructor to the Cat class:
Now we must declare the destructor:
Thus the destructors can be added to the main function:
The above example is pointless. The main use of destructors (in my opinion) is to delete any dynamic memory you may have allocated in the lifespan of the class, a quick example to show this:
Destructors can take no parameter and they cannot return a value
It is defined like the constructor except it starts with a squiggle ~
i.e.
Constructor = Cat()
Destructor = ~Cat()
Though the example I'm about to do is pointless, it will help explain a destructor.
Let's say when we want to delete a Cat object, we want to set its name to "" - a blank string
First we add the destructor to the Cat class:
- Code:
class Cat
{
private:
string Name;
public:
//constructor
Cat(string Name);
//destructor
~Cat();
void Meow();
void Hiss();
void Bite(Cat catThatIsBitten);
void SetName(string name);
string GetName() const;
};
Now we must declare the destructor:
- Code:
Cat::~Cat()
{
this->Name = "";
}
Thus the destructors can be added to the main function:
- Code:
int main()
{
Cat Tibbles("Tibbles");
Cat Snowy("Snowy");
Tibbles.Meow();
Snowy.Hiss();
Snowy.Bite(Tibbles);
cin.get();
Snowy.~Cat();
Tibbles.~Cat();
return 0;
}
The above example is pointless. The main use of destructors (in my opinion) is to delete any dynamic memory you may have allocated in the lifespan of the class, a quick example to show this:
- Code:
class Foo
{
int *Arr; //integer array
public:
Foo();
~Foo();
};
Foo::Foo()
{
//allocate memory
Arr = new int[100];
}
Foo::~Foo()
{
//delete memory
delete[] Arr;
}
int main()
{
Foo f;
//f has an array of 100 integers allocated to it
//do stuff with f
//clear up the dynamic memory before we return (good practice)
f.~Foo();
return 0;
}
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes and objects
If anyone's interested + I can be bothered I'll carry on with
- Constructor Overloading
- Inheritance
- Operator Overloading
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
RUSES :: Part 1 :: Programming
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum