In object-oriented programming (OOP), classes are blueprints for creating objects. A class defines a set of properties (data members) and behaviors (member functions) that the created objects will have. Understanding classes and objects is fundamental to mastering OOP in C++.
Classes encapsulate data and functions that operate on that data, providing a way to organize code logically. Objects are instances of classes, and they hold actual values for the data members defined by the class. This tutorial will guide you through defining classes, creating objects, understanding data members, member functions, and the special this pointer.
A class is defined using the class keyword followed by the class name and a pair of curly braces {} containing the class members. Here’s a simple example:
1#include <iostream>2using namespace std;34// Define a class named Car5class Car {6public:7// Data members8string brand;9int year;1011// Member function to display car details12void display() {13cout << "Brand: " << brand << ", Year: " << year << endl;14}15};1617int main() {18// Create an object of the Car class19Car myCar;2021// Assign values to data members22myCar.brand = "Toyota";23myCar.year = 2023;2425// Call member function26myCar.display();2728return 0;29}
Brand: Toyota, Year: 2023
class Car defines a blueprint for creating car objects. It includes two data members (brand and year) and one member function (display()).main() function, an object named myCar is created from the Car class..), e.g., myCar.brand = "Toyota";.myCar.display();.Data members are variables that hold data specific to an object. They can be of any data type and are defined within the class. Here’s a more detailed example:
1#include <iostream>2using namespace std;34class Student {5public:6string name;7int age;8double gpa;910void display() {11cout << "Name: " << name << ", Age: " << age << ", GPA: " << gpa << endl;12}13};1415int main() {16Student student1;17student1.name = "Alice";18student1.age = 20;19student1.gpa = 3.8;2021student1.display();2223return 0;24}
Name: Alice, Age: 20, GPA: 3.8
name, age, and gpa are data members of the Student class.Member functions are functions that belong to a class. They can access and modify the data members of an object. Here’s an example with member functions:
1#include <iostream>2using namespace std;34class Rectangle {5public:6int width;7int height;89// Constructor10Rectangle(int w, int h) {11width = w;12height = h;13}1415// Member function to calculate area16int area() {17return width * height;18}19};2021int main() {22Rectangle rect(5, 3);23cout << "Area: " << rect.area() << endl;2425return 0;26}
Area: 15
area() calculates and returns the area of the rectangle.this PointerThe this pointer is a keyword in C++ that points to the current object being used. It is particularly useful when there are local variables or parameters with the same name as data members. Here’s an example:
1#include <iostream>2using namespace std;34class Point {5public:6int x, y;78// Constructor9Point(int x, int y) {10this->x = x; // Using 'this' to distinguish between member and parameter11this->y = y;12}1314void display() {15cout << "Point (" << x << ", " << y << ")" << endl;16}17};1819int main() {20Point p(10, 20);21p.display();2223return 0;24}
Point (10, 20)
this Pointer: Used to differentiate between the object’s data members (x, y) and the constructor parameters (x, y).Let’s create a complete program that models a bank account with deposit and withdraw functionalities:
1#include <iostream>2using namespace std;34class BankAccount {5public:6string owner;7double balance;89// Constructor10BankAccount(string o, double b) : owner(o), balance(b) {}1112// Member function to deposit money13void deposit(double amount) {14if (amount > 0) {15balance += amount;16cout << "Deposited: $" << amount << endl;17} else {18cout << "Invalid deposit amount." << endl;19}20}2122// Member function to withdraw money23bool withdraw(double amount) {24if (amount > 0 && amount <= balance) {25balance -= amount;26cout << "Withdrew: $" << amount << endl;27return true;28} else {29cout << "Insufficient funds or invalid withdrawal amount." << endl;30return false;31}32}3334// Member function to display account details35void display() {36cout << "Owner: " << owner << ", Balance: $" << balance << endl;37}38};3940int main() {41BankAccount acc("John Doe", 1000.0);42acc.display();4344acc.deposit(500.0);45acc.withdraw(200.0);4647acc.display();4849return 0;50}
Owner: John Doe, Balance: $1000 Deposited: $500 Withdrew: $200 Owner: John Doe, Balance: $1300
BankAccount class with data members owner and balance.deposit(), withdraw(), and display() to manage and display account details.| Concept | Description |
|---|---|
| Class | Blueprint for creating objects. |
| Object | Instance of a class with actual values for data members. |
| Data Members | Variables that hold data specific to an object. |
| Member Functions | Functions that operate on the data members of an object. |
this Pointer | Points to the current object, useful for distinguishing between member variables and parameters. |
In the next tutorial, we will explore more about class methods, including constructors, destructors, and operator overloading. Understanding these concepts will further enhance your ability to design and implement robust C++ programs using OOP principles.
Stay tuned!