Introduction to C++ Classes and Objects

COS1511 - Introduction To Programming I · Learning C++

Introduction to C++ Classes and Objects

C++ is an object-oriented programming language. This means that it allows you to create classes and objects. A class is a blueprint for creating objects. An object is an instance of a class. Understanding classes and objects is essential for developing complex programs.

What is a Class?

A class in C++ is a user-defined data type. It can contain data members (variables) and member functions (methods) that operate on the data. You define a class using the class keyword. Here is a simple example:

class Car {   // Class definition   public:     string brand;     string model;     int year;     void display() {       cout << brand << " " << model << " " << year << endl;     } };

In this example, the class Car has three data members: brand, model, and year. It also has a member function display() that prints the car's details.

Creating Objects

Once you define a class, you can create objects of that class. You create an object using the following syntax:

Car myCar;

Here, myCar is an object of the class Car. You can access the data members and member functions of the object using the dot operator (.). For example:

myCar.brand = "Toyota";  // Assigning values to data members myCar.model = "Corolla"; myCar.year = 2020; myCar.display();  // Calling the display function

This code assigns values to the data members of the object myCar and then calls the display() function to print the car's details.

Access Modifiers

Access modifiers control the visibility of class members. There are three types of access modifiers:

  • public: Members declared as public can be accessed from outside the class.
  • private: Members declared as private can only be accessed within the class.
  • protected: Members declared as protected can be accessed in the class and by derived classes.

By default, members of a class are private if no access modifier is specified. Here is an example:

class Person {   // Class definition   private:     string name;     int age;   public:     void setName(string n) {       name = n;     }     void setAge(int a) {       age = a;     }     void displayInfo() {       cout << "Name: " << name << ", Age: " << age << endl;     } };

Remember: Use public members for data that should be accessible from outside the class. Use private members for data that should be hidden.

Constructors and Destructors

A constructor is a special member function that is called when an object is created. It has the same name as the class and does not have a return type. A destructor is called when an object is destroyed. It also has the same name as the class but is preceded by a tilde (~).

class Book {   public:     Book() {       cout << "Book created." << endl;     }     ~Book() {       cout << "Book destroyed." << endl;     } };

In this example, when a Book object is created, the constructor prints "Book created." When the object is destroyed, the destructor prints "Book destroyed."

Member Functions

Member functions can be defined inside or outside the class definition. Here is an example of defining a member function outside the class:

class Rectangle {   public:     int width, height;     int area(); };  int Rectangle::area() {   return width * height; }

In this example, the function area() is defined outside the class. You can call it like this:

Rectangle rect; rect.width = 5; rect.height = 10; cout << "Area: " << rect.area() << endl;

Inheritance

Inheritance is a feature that allows a class to derive properties and behaviour from another class. The derived class inherits members from the base class. Here is an example:

class Animal {   public:     void eat() {       cout << "Eating..." << endl;     } };  class Dog : public Animal {   public:     void bark() {       cout << "Barking..." << endl;     } };

In this example, the class Dog inherits from the class Animal. The Dog class can use the eat() method defined in the Animal class.

Watch out: When using inheritance, ensure that you understand the access levels of base class members in the derived class.

Polymorphism

Polymorphism allows methods to do different things based on the object that it is acting upon. This is often achieved through function overriding and function overloading. Here is an example of function overriding:

class Shape {   public:     virtual void draw() {       cout << "Drawing a shape" << endl;     } };  class Circle : public Shape {   public:     void draw() {       cout << "Drawing a circle" << endl;     } };

In this example, the method draw() is overridden in the Circle class. When you call draw() on a Circle object, it will execute the Circle version of the method.

Summary

  • A class is a blueprint for creating objects.
  • Objects are instances of classes.
  • Access modifiers control the visibility of class members.
  • Constructors and destructors manage the lifecycle of objects.
  • Inheritance allows one class to inherit properties from another class.
  • Polymorphism enables methods to behave differently based on the object.

Check your understanding

  1. What is the difference between a class and an object?
  2. Explain the purpose of constructors and destructors.
  3. How does inheritance work in C++?
  4. What is polymorphism, and how is it achieved in C++?