C++ Classes and Objects
A class bundles data and functions. private members are hidden; public members are the interface. The constructor sets an object up.
ExampleC++
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
void birthday() { age++; }
void print() const { cout << name << " is " << age << endl; }
};
int main() {
Person p("Ada", 36);
p.birthday();
p.print();
return 0;
}Output
Ada is 37
Quick check
Which access level hides members from outside code?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.