Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Classes and Objects

User image

Published by

sanya sanya

Published at: 6th Aug, 2023
3.96 mins read

Introduction

Classes and objects are the fundamental building blocks of object-oriented programming (OOP). A class is a user-defined data type that serves as a blueprint for creating objects. It encapsulates data (attributes) and functions (methods) that define the behavior and characteristics of the objects. Objects, on the other hand, are instances of a class, representing individual entities with their own state and behavior. OOP promotes the concept of encapsulation, abstraction, inheritance, and polymorphism to create organized and modular code.

Objects

An object is an instance of a class. Using the blueprint specified by the class, it represents a particular entity. The member functions defined in the class can be called by each object, each of which has its own set of data members. It is simpler to organize and manage data and behavior when you can work with real-world entities or abstract concepts in your code thanks to objects.

Syntax:

Class_name object_name;

Example:

class Student {

public:

int roll;

int marks;

void setmarks(int newroll, int newmarks){

roll=newroll;

marks=newmarks;

}

}

int main(){

//Creating objects

Student s1;

Student s2;

//Calling member functions

s1.setmarks(1, 98);

s2.setmarks(2,92);

}

Hence, in the above example ‘s1’ and ‘s2’ are the objects or instances of class ‘Student’. We can also call the member function using object.

Creating classes

To create a class, use the ‘class’ keyword followed by the name of the class and the class body enclosed in curly braces. The class body contains the class members, such as data members and member functions. Data members represent the attributes of the class, and member functions define its behavior.

Example:

// Example of a simple class in C++

class MyClass {

public:

// Data members

int myData;

// Member function

void display() {

cout << "Value of myData: " << myData << endl;

}

};

Class members

Class members are the data members and member functions defined within a class. They are accessed using the dot operator (.) on objects of the class. There are three types of class members:

- Public Members: Public members are accessible from outside the class. They form the public interface of the class, and other parts of the program can use them to interact with objects of the class.

- Private Members: Private members are only accessible within the class itself. They are hidden from the outside world, and access to them is restricted to member functions of the class.

- Protected Members: Protected members are similar to private members, but they are accessible in derived classes (inherited classes) as well.

Example:

class MyClass {

public: // Public section

int publicData;

private: // Private section

int privateData;

protected: // Protected section

int protectedData;

public:

void publicMethod() { /* ... */ }

void privateMethod() { /* ... */ }

void protectedMethod() { /* ... */ }

};

Constructors and Initialization

Constructors are special member functions that are automatically called when an object is created. They are responsible for initializing the object's state and allocating resources, if necessary. Constructors have the same name as the class and no return type.

- Default Constructor: If no constructor is defined, the compiler generates a default constructor that does nothing (if there are no data members to initialize).

- Parameterized Constructor: A constructor that takes parameters to initialize the object with specific values.

- Initialization List: Initialization lists in constructors allow you to initialize data members directly rather than in the body of the constructor.

Example:

class MyClass {

public:

int value;

// Default Constructor

MyClass() {

value = 0; // Initialize value to 0

}

// Parameterized Constructor

MyClass(int val) {

value = val; // Initialize value with the provided parameter

}

// Constructor with Initialization List

MyClass(int val) : value(val) {

// value is initialized directly in the initialization list

}

};

This keyword

The ‘this’ keyword is a pointer that refers to the current object inside a member function of a class. It is used to differentiate between the class's data members and parameters or local variables with the same name. It allows you to access the current object's members explicitly.

Example:

class MyClass {

private:

int data;

public:

void setData(int data) {

this->data = data; // Use "this" to refer to the data member of the class

}

};

In the above example, ‘this->data’ refers to the data member of the class, while ‘data’ without ‘this’ refers to the parameter of the function ‘setData’.

Library

WEB DEVELOPMENT

FAANG QUESTIONS