Lesson 51Object-Oriented Programming
Classes & Objects
Learn about classes & objects in C++ programming!
main.cpp
#include <iostream>
#include <string>
using namespace std;
class Pet {
private:
string name;
int age;
public:
// Constructor
Pet(string n, int a) {
name = n;
age = a;
}
// Methods
void introduce() {
cout << "Hi! I'm " << name;
cout << " and I'm " << age << " years old!" << endl;
}
void birthday() {
age++;
cout << name << " is now " << age << "!" << endl;
}
};
int main() {
Pet myPet("Buddy", 3);
myPet.introduce();
myPet.birthday();
return 0;
}