C++ Classes & Objects

C++ Classes & Objects

ยท

1 min read

Classes & Objects

Classes are used to specify the arrangement of objects and it combines data representation and methods for using and manipulating that data into one organised bundle. The class provides the blueprint for objects, so basically an object is created from a class and the data & functions in the class are called members of that class. Following code is an example on class declaration and making an object:

#include <iostream>
using namespace std;

    class ClassName {
           public:
             void function_name(int age){
              cout<< "Hellon\I'm Tokelo and I am " << age<< "years old:)" << endl;
    }
     };

   int main(){
      ClassName ObjectName;
       OjectName.function_name(20);
        return 0;
}

Declaring a class

A class is declared by typing classand then the name of the class and the we have an open, closed curly brackets and semicolon. In between the curly brackets we need to have a access specifier, this could be private, protected or public. These specifiers give access rights that the members following them get.

Declaring objects

We declare objects of a class with exactly the same sort of declaration that we declare variables of the basic data types. The public data members of objects can be accessed using the direct member access operator (.). Each object made will have their own copy of data members.

Follow me on twitter ๐Ÿš€ twitter.com/t0kel0

ย