...

/

Introduction To Classes

Introduction To Classes

Learn about Classes using object-oriented Methodology.

Building blocks of Object Oriented Programming

Classes are the building blocks of programs built using the object-oriented methodology. Such programs consist of independent, self-managing modules and their interactions. An object is an instance of such module created at run time and a class is its definition.

Class definition and instantiation

A class definition includes the declaration of its data members (data variables) and member functions (class functions) , whereas instantiation is the process of creating an actual object. This object can then be used to access the data members and member functions defined in the class.

Press + to interact
class className
{
variable var1 // variables names can be of type string, int, float
variable var2
}; // class body always terminates with ';'

A Keyword class is used with every declaration of class followed by the name of the class. You can use any className as you want as long as it follows the naming rules that we defined previously in the context of variables.

We can also initialize our class data members at the time of declaration like we can do with simple varaibles. Here's how we can initialize our data members with default values for class demoClass.

Press + to interact
class demoClass
{
int var1 = 25;
char name[7] = "nimra"
};

Let’s look at an example where we create a Dog class and use it to learn about classes in detail.

Press + to interact
class Dog{
public:
char name[25];
char gender[8];
int age;
int size;
bool healthy;
};
int main(){
Dog dogObj; // creating an object of Dog class called DogObj
}

Members

...