Basic C++ OOP .. Intimidating?

Yesterday night I continued to read my C++ Book, “Sams teach yourself C++ in 21 days”. This book is very good and I recommend it to anyone that wants to learn C++. In chapter 6 they start talking about OOP ( Object Oriented Programming ). C++ has a new feature called “Classes”. This is a very important feature because it gives you the ability to program like if the objects where real. This is a code I wrote with c++ / classes.

#include <iostream>

using std::cout;

class Pet
{
public:
int getAge();
void setAge(int age);
int getFood();
void setFood(int food);
private:
int itsAge;
int itsFood;
};

int Pet::getAge()
{
return itsAge;
}

void Pet::setAge(int age)
{
itsAge = age;
}

int Pet::getFood()
{
return itsFood;
}

void Pet::setFood(int food)
{
itsFood = food;
}

int main()
{
Pet Patrick;

Patrick.setAge(1);

cout << “\n\nPatrick is a pet that is ” << Patrick.getAge() << ” Year old.\n\n”;

Patrick.setAge(16);

cout << “15 years later patrick is ” << Patrick.getAge() << ” Years old.\n\n”;

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: