C++

Class

C++ is an object oriented programming.

Advantages of Object Oriented Programming over Procedural Programming

Enhanced Data Security (Encapsulation): OOP bundles data and methods inside a class, making data private by default. It restricts direct access and prevents unintended modifications from outside functions, a common vulnerability in procedural programming where data is often global.

Superior Code Reusability (Inheritance): Subclasses can inherit fields and methods from parent classes. This allows developers to reuse existing code structures without rewriting them, heavily enforcing the DRY (Don't Repeat Yourself) principle. Procedural reuse is limited to calling isolated functions.

System Flexibility (Polymorphism): Polymorphism allows different objects to execute unique behaviors through the same interface or method call. This eliminates the complex, nested conditional statements (if-else or switch) typically required in procedural code to handle varying data types.

Reduced Complexity (Abstraction): OOP hides complex implementation details and exposes only essential interfaces. Developers interact with what an object does rather than how it does it, making the overall codebase simpler to comprehend.

C++ is associated with classes and objects, along with its attributes and methods.

For example: in real life, a human is an object. The human has attributes, such as height,weight,race,eye colour and methods, such as eat, run,sleep, etc.

Attributes and methods are basically variables and functions that belongs to the class. Method is therefore simply a function in a class.

Attributes and methods are often referred to as "class members".

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 a "blueprint" for creating objects

The keyword class is used to create a class.

The syntax of a class in C++ is:

class Car {  // The class
    public:  // Access specifier
    string brand;        // Attribute (string variable)
    string model;  // Attribute (string variable)
    int year;  // Attribute (int variable)
};

The class keyword is used to create a class called Car.

The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class. We will learn more about access specifiers later.

Inside the class, there is an integer variable year and a string variable brand. When variables are declared within a class, they are called attributes.

At last, end the class definition with a semicolon ;.

Creating object of a class

An object is an instance of a class.

The syntax below is used to create an object of a class:

ClassNAME objectName;

Class names usually starts with uppercase and object name with lowercase.

An object for the Car class that we created earlier on could be written as follows:

Car car;

A method is a function in a class. We've already learned about function in our previous lesson.

In our earlier lesson on function, we wrote c++ code for mathematical functions. We will therefore create a class called Mathematics and include those functions as the methods of the class.

In addition, we will have, have a variable to set a name and welcome the person to C++

#include  <iostream>
using namespace std;
class Mathematics{
   public:
   string name;
   void welcome(){
      cout << "Welcome " << name << " to c++" << endl;
   }
   double sum(double a, double b){
    return a + b;
   }
   double multiply(double a, double b){
    return a * b;
   }
   double subtract(double a, double b){
    return a-b;
   }
   double divide(double a, double b){
    return a/b;
   }
};

Assigning values to variables and calling methods in a class

To assign a value to variable or call a method (using a function in a class), . is used after the object name

Note: to assign a value to a variable in an object, you have to use the public access specifier

Let's add an int main function to the above code and create object of the class, assign value to the name variable and call the methods in the class.


#include <iostream>
#include <string>
using namespace std;
class Mathematics{
   public:
   string name;
   void welcome(){
      cout << "Welcome " << name << " to c++" << endl;
   }
   double add(double a, double b){
    return a + b;
   }
   double multiply(double a, double b){
    return a * b;
   }
   double subtract(double a, double b){
    return a-b;
   }
   double divide(double a, double b){
    return a/b;
   }
};
int main(){
    Mathematics maths;
    string name;
    cout << "Enter your name: ";
    cin >> name;
    maths.name = name;
    maths.welcome();
    double a,b;
    cout << "Enter a number: ";
    cin >> a;
    cout << "Enter another number: ";
    cin >> b;
    cout << a << " + " << b << " = " << maths.add(a,b) << endl;
    cout << a << " x " << b << " = " << maths.multiply(a,b) << endl;
    cout << a << " - " << b << " = " << maths.subtract(a,b) << endl;
    cout << a << " / " << b << " = " << maths.divide(a,b) << endl;
}

As you can see in the above code, we assigned the value to the name variable using the code:

maths.name
where maths is the object and name is the variable declared in the Mathematics class.

The methods in the class were called as:


maths.add(a,b)
maths.multiply(a,b)
maths.subtract(a,b)
maths.divide(a,b)

Thus, the object name followed by a full stop (.) then the method