In this C++ tutorial, we will learn about the fundamental concept of object-oriented programming, i.e., classes and objects. Classes are the blueprint of objects, and objects are instances of classes. We will understand the process of defining classes, creating objects, and manipulating these objects.
By the end of this tutorial, you will learn:
- What are classes and objects?
- How to define a class?
- How to create and use objects?
Prerequisites: Basic knowledge of C++ programming.
A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. A class is defined using the keyword class
, followed by the class name.
class ClassName {
// class body
};
An object is an instance of a class. When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
ClassName objectName;
A class contains both variables (attributes) and functions/methods.
class Car {
public:
string brand; // attribute
int year; // attribute
void honk() { // method
cout << "Beep beep!\n";
}
};
#include <iostream>
#include <string>
using namespace std;
// Define a class
class Car {
public:
string brand;
int year;
};
int main() {
// Create an object of Car
Car carObj1;
carObj1.brand = "Toyota";
carObj1.year = 2015;
// Access attributes and display the value
cout << carObj1.brand << " " << carObj1.year << "\n";
return 0;
}
In the above example, we first define a Car
class with two attributes: brand
and year
. Then, we create an object carObj1
for the Car
class and assign values to its attributes. Finally, we display these attribute values.
Expected Output: Toyota 2015
In this tutorial, we learned about classes and objects in C++. We defined a class using the class
keyword and created objects to access the data members and functions. We also learned how to access these members and functions using the dot operator (.
).
Next steps for learning include understanding concepts like constructors, destructors, and inheritance in C++. For additional resources, refer to C++ Documentation.
Exercise 1: Define a class Person
with two attributes: name
and age
. Create an object of Person
, set its attribute values and display them.
Exercise 2: Add a function birthday()
to the Person
class that increases the age by 1 when called. Create an object, increase its age using the function, and display its new age.
Solutions
#include <iostream>
#include <string>
using namespace std;
// Define class Person
class Person {
public:
string name;
int age;
};
int main() {
// Create an object of Person
Person personObj;
personObj.name = "John";
personObj.age = 25;
// Access attributes and display the value
cout << personObj.name << " " << personObj.age << "\n";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
// Define class Person
class Person {
public:
string name;
int age;
void birthday() {
age++;
}
};
int main() {
// Create an object of Person
Person personObj;
personObj.name = "John";
personObj.age = 25;
// Call birthday method
personObj.birthday();
// Access attributes and display the value
cout << personObj.name << " " << personObj.age << "\n"; // Expect "John 26"
return 0;
}
In the second exercise, we've added a birthday()
function to the Person
class that increases the age by 1 when it's called. We then called this function on our personObj
to increase its age before displaying it.
Happy coding!