Classes and Objects
A class defines a new type. An object is an individual instance of that class. Classes allow related data and functions to be kept together.
Create a Class
Student class
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
string name;
int age;
void display() const
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main()
{
Student student;
student.name = "Alice";
student.age = 20;
student.display();
return 0;
}Class and Object
| Term | Meaning |
|---|---|
| Class | A blueprint that defines data and behaviour. |
| Object | A usable instance created from a class. |
| Member variable | A value stored inside an object. |
| Member function | A function that belongs to the class. |
Practice task
Create a Product class with a name, price, and a function that displays both values.
Lesson Summary
Create your first class and use objects to combine data with behaviour.