Code:
class Dog[edit]
Structs are containers whose properties are always public. In order to gain control over what is public and what isn't, we will have to use a class. Let us see how the Dog class would look like in a class instead of a struct:
#include <string>
class Dog
{
private:
std::string name;
int gender;
int age;
int size;
bool healthy;
};
Now our dogs precious properties aren't public for everyone to view and change. However, this raises a little problem. We can't change them either. Only other dogs can see and change them. In order to be able to access a Dog's private properties, we would have to make a function to do so. This brings us to our next chapter.
Dogs with Methods[edit]
Classes can have functions in them. These functions are called methods. Methods can access all, and even private, properties and methods (yes, methods can be private) of its class. Let us make some methods to get our dog's information...
#include <string>
class Dog
{
private:
std::string name;
char gender;
int age;
int size;
bool healthy;
public:
std::string getName() const { return name; }
int getGender() const { return gender; }
int getAge() const { return age; }
int getSize() const { return size; }
bool isHealthy() const { return healthy; }
void setHealthy(bool dhealthy) { healthy = dhealthy; }
void setName(const std::string& dname) { name = dname; }
};
#include <iostream>
int main()
{
Dog lucy;
std::cout << "lucy's name is " << lucy.getName() << std::endl;
std::cout << "lucy's gender is " << lucy.getGender() << std::endl;
std::cout << "lucy's age is " << lucy.getAge() << std::endl;
std::cout << "lucy's size is " << lucy.getSize() << std::endl;
if(lucy.isHealthy())
std::cout << "lucy is healthy" << std::endl;
else
std::cout << "lucy isn't healthy :(" << std::endl;
std::cout << "Now I'm changing lucy abit..." << std::endl;
lucy.setHealthy(!(lucy.isHealthy()));
lucy.setName("lUCY");
std::cout << "lucy's name is " << lucy.getName() << std::endl;
std::cout << "lucy's gender is " << lucy.getGender() << std::endl;
std::cout << "lucy's age is " << lucy.getAge() << std::endl;
std::cout << "lucy's size is " << lucy.getSize() << std::endl;
if(lucy.isHealthy())
std::cout << "lucy is healthy" << std::endl;
else
std::cout << "lucy isn't healthy :(" << std::endl;
return 0;
}
However, if we run this program, we might have a little problem. We have never initialized lucy's properties...
Constructors[edit]
In order to initialize lucy's properties, we could write a method that would do it for us, and call it every time we make a dog. However, C++ already provides us such a method. A constructor is a method which is called every time a new object is created. To use this nice little thing provided to us by C++, we will have to write a method which returns no value, and has the same name as the class. Here's Dog written using a constructor:
#include <string>
class Dog
{
private:
std::string name;
char gender;
int age;
int size;
bool healthy;
public:
std::string getName() const { return name; }
char getGender() const { return gender; }
int getAge() const { return age; }
int getSize() const { return size; }
bool isHealthy() { return healthy; }
void setHealthy(bool dhealthy) { healthy = dhealthy; }
void setName(const std::string& dname) { name = dname; }
Dog() :
name("Lucy"), gender('f'), age(3), size(4), healthy(true)
{}
};
From now on, every dog we instantiate will have the name "Lucy", be of gender 1, aged 3, sized 4 and healthy. Well, what if you want some diversity? No problem. Constructors with parameters!
Dog(const std::string& dname, int dgender, int dage, int dsize, bool dhealthy) :
name(dname), gender(dgender), age(dage), size(dsize), healthy(dhealthy)
{}
If you have both constructors in your Dog class, you can now either create a dog as always, which will have the default values (named "Lucy" etc etc), or you could use RAII to make your customized dog as follows:
#include <iostream>
int main()
{
Dog scruffy("Scruffy", 'm', 8, 6, false);
std::cout << "scruffy's name is " << scruffy.getName() << std::endl;
std::cout << "scruffy's gender is " << scruffy.getGender() << std::endl;
std::cout << "scruffy's age is " << scruffy.getAge() << std::endl;
std::cout << "scruffy's size is " << scruffy.getSize() << std::endl;
if(scruffy.isHealthy())
std::cout << "scruffy is healthy" << std::endl;
else
std::cout << "scruffy isn't healthy :(" << std::endl;
return 0;
}