C++ Classes and Objects

C++ classes and objects are the main components of object oriented programming. Classes are user defined that contain methods and attributes. An object is blue print of the class. Class contains the properties of that object. For example, vehicle is a class that contains the definition for all types of vehicles. The types of vehicles like car, truck, etc. are the instances of vehicle class. Instead of defining each type separately, classes help to select the specific properties for each type of vehicle from main class.
Classes consist of class members. There are two members of a class:

  • Variables
  • Functions

The properties or attributes of a class are its variables. For example, the model, color or type of vehicle are its variables. Whereas, the methods of the class are its functions. Methods like steer right, left, stop, drive are functions of vehicle class.

Defining C++ Class

The keyword class is used to define a class, followed by class name and curly braces. The curly braces {} contain the class members (variables and functions). The class definition ends at semi colon, after the curly braces. The syntax for defining C++ class is:

For example, the below code shows how to define a simple class naming Book:

Class name acts as an identifier for class. Here public is a keyword that defines access type of class. It is called Access Specifier as it specifies whether the variables and functions are accessible only within the class or outside the class as well. The class Book contains three variables (title, author, pages). The variable title and author are of type string while the variable pages is of type int. Each variable terminates at semi colon. These variables are called class attributes.

Defining C++ Objects

Programmer can create multiple objects of a class. The syntax for defining class objects is writing class name following object name.

For example, an object of class Book can be defined as:

Accessing Class Variables

After creating objects of class, programmer can assign values to its variables. For that programmer first needs to access the variables. The syntax for accessing class attributes is writing object name and variable name with dot operator (.) between the two.

For example, in order to access the variable title of the object story_book, the code will be.

Programmer can assign values to object variables using assignment operator (=). For example, the below code shows how to assign values to variables of object and print them.

// This code is written by www.tutorialsart.com
#include <iostream>
#include <string>
using namespace std;
class Book {    
  public:
    string title;
    string author;
    int pages;
};
int main() {
  Book story_book;
  story_book.title = "Jack and the bean stalk"; 
  story_book.author = "Robert James";
  story_book.pages = 25;
  cout << story_book.title << endl;
  cout << story_book.author << endl;
  cout << story_book.pages;
  return 0;
}

Here the class is Book having variables title, author and pages. story_book is the object of class Book.

The output of above code after compilation is:

Accessing Class Functions

There are two ways to define function for a class. Functions of class can be defined within a class or outside the class. Programmers can access functions of the class by using dot operator (.). The syntax of calling class function is writing object name with function name with dot operator (.) in between.

Below is another example, that shows functions of a class. There is function bookDetails defined inside the class that prints details of the book.

#include <iostream>
#include <string>
using namespace std;
class Book {    
  public:
    string title;
    string author;
    int pages;
    void bookDetails()
  {
  cout << title << endl;
  cout << author << endl;
  cout << pages;
  }
};
int main() {
  Book story_book;
  story_book.title = "Jack and the bean stalk"; 
  story_book.author = "Robert James";
  story_book.pages = 25;
  story_book.bookDetails();
  return 0;
}

The output of above code after compilation is:

A function can also be defined outside the class by using resolution operator (::). The syntax for defining function outside the class is writing class name followed by resolution operator, followed by function definition. Note that if the function definition is outside the class then it should be declared inside the class definition. For example,

#include <iostream>
#include <string>
using namespace std;
class Book {    
  public:
    string title;
    string author;
    int pages;
    void bookDetails();
};
void Book::bookDetails()
  {
  cout << title << endl;
  cout << author << endl;
  cout << pages;
  }
int main() {
  Book story_book;
  story_book.title = "Jack and the bean stalk"; 
  story_book.author = "Robert James";
  story_book.pages = 25;
  story_book.bookDetails();
  return 0;
}

The output of above code after compilation is:

The functions that are defined inside the class definition are inline functions. A class can have more than one functions. For example, in the code below there is a class addOperation. The class addOperation has two functions that take sum of two and three variables respectively.

#include <iostream>
using namespace std;
class addOperation {
public:
    int addition(int x,int y) {
        int sum;
        sum = x + y;
        cout << "Sum of two numbers is: " << sum << endl;
        return 0;
    }
    double addition(double x,double y, double z) {
        double sum;
        sum = x + y + z;
        cout << "Sum of three float numbers is: " << sum << endl;
        return 0;
    }
};
int main() 
{
    addOperation add1;
    add1.addition(10, 20);
    add1.addition(10.2, 20.2, 30.2);
    return 0;
}

The output of above code after compilation is:

The members of a class can be public or private. These are accsess specifiers of a class. Programmer can access public members of a class from outside the class and inside the class as well. While the private members of a class are only accessible from within the class.

For example, the function bookDetails in above examples is accessible from everywhere in the program. Here is another example that shows a class having private members. The variables of the class Book are private hence user cannot access them from the int main() function. Doing so, will give error in compilation. The functions of the class Book are public hence they are accessed from the int main() function.

#include <iostream>
#include <string>
using namespace std;
class Book {
   private:
    string title;
    string author;
    int pages;
   public:
    void getData(string t, string a, int p) {
        title = t;
        author = a;
        pages = p;
    }
    void bookDetails(){
        cout << title << endl;
        cout << author << endl;
        cout << pages;
    }
};
int main() {
    Book book1;
    book1.getData("Harry Potter", "JK Rowling", 500);
    book1.bookDetails();
    return 0;
}

The output of above code after compilation is:

#include <iostream>
#include <string>
using namespace std;
void dispFunc(int x) {
    cout << "Displaying int value: " << x << endl;
    }
void dispFunc(double  y) {
    cout << "Displaying float value: " << y << endl;
    }
void dispFunc(string z) {
    cout << "Displaying string: " << z << endl;
    }
int main() 
{
   dispFunc(33.2);
   dispFunc(62.223);
   dispFunc("C++ Overloading Functions");
   return 0;
}