GAME OF CODE’22 — DAY 5

Hello, coders hope you are reading the previous blogs and enjoying them. In this blog, you were going to learn about the super interesting concept of C++ is object-oriented programming.

Oops stands for object-oriented programming language. It is a set of concepts that are used to solve specific types of problems. Here things revolve around object because that bring the program closer to the real world. It is all about creating objects with both data and functions.

Now you obviously understand that C++ is an object-oriented programming language.

Characteristics of oops:

· Class

· Object

· Encapsulation

· Inheritance

· Polymorphism

· Abstraction

So Let’s meet with the concept of OOPS :

What is Classes and object in C++?

User-defined datatype consists of data member and member functions. Data member and member functions can be accessed and used by creating an instance of the class. Data members are the variable declared in any class by using fundamental data types. And functions which are declared either private or public section known as a member function.

An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

You will find yourself surrounded by a number of objects which have certain characteristics and behaviors.

For example, we can say ‘Orange’ is an object. Its characteristics are: spherical in shape and color is orange. Its behavior is: juicy and tastes sweet-sour.

For example, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc. And its behavior is mobility. But we can say Honda City has a reg. number 4654 is an ‘object’ that belongs to the class ‘car’.

Why do we use the concept of class if the structure is already available 🤔?

Because we want data hiding, we want some data to remain private and some to be accessible. But with structure data can be easily accessible without any kind of restrictions. The structure is not safe.

Now it’s time to know about access specifiers 💫.

There are three kinds of access specifiers i.e., public, private, protected. By default, the c++ class is private means if no access specifiers are mentioned then the class is considered a private class.

Now it really means you know why we use the concept of class………

Private: Accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Public: Easily accessible from everywhere, accessed by other classes also.

Protected: It can’t be accessed from outside but it can be accessed by any subclass /derived class of a class.

Here we can’t access y because y is in a private class.

But is the private class never accessed by the object?? Yes, by using the set and get function it can be accessed as

Setdata function sets the data and getdata function prints the data. Now we can access the private data.

Class is a blueprint/template of an object.

Hola geeks focus here 💥, name, id, marks, section are data members (or attributes).

Student one;

One.name=’shruti’;

One.id=7029;

One. Section=’a’;

One. Marks=1;

Here ‘one’ is an object that is an instance of class ‘student’, attributes are accessed by dot operator.

To define a function outside the class definition, you have to declare it inside the class and then define it outside of the class. This is done by specifying the name of the class, followed the scope resolution :: operator, followed by the name of the function:

Here, in this code, you may have doubt that how it is possible, declare a function inside the class and define from outside class🤔?

So this problem can be solved by SCOPE RESOLUTION OPERATOR (: :) with naming function, scope resolution operator access global variables

For knowing more go to:

For practicing questions go to:

ENCAPSULATION:

Hiding sensitive data from the user, binds the data together and after that manipulates the function. It also leads to data abstraction. It wraps similar data and functions in one place.

Access specifiers play a vital role in implementing encapsulation in c++. A private class can be accessed by getting and setting functions.

DATA ABSTRACTION:

Abstraction is a process used to hide essential data. It only shows information that is useful to the user and hides the internal details.

e.g.,

Here, the marks are only accessible by class members, and the function which is not class members are not able to access it. Marks are hidden for outside function.

CONSTRUCTOR:

This is a member function that is used to initialize the value to data members and has the same name as the class name.

Constructors are automatically called whenever we made objects of a class.

There are mainly four types of constructors: -

Ø Parametrised

Ø Default

Ø Copy

If parameters are passed during object creation, the parameterized constructor is called.

If the parameters are not passed when the object is created, the default constructor is called.

If the creation of a new object as a copy of the existing object is needed then copy constructor is called, it is also known as a special constructor.

In this, student class for student a: parametrized constructor passed, for student b: default constructor passed, for student c: copy constructor passed that copy all values of an in c.

There are two types of copy constructors.

Ø Shallow copy: comes under default copy constructor

Ø Deep copy: comes under copy constructor.

In shallow copy, the object can be copied to another object but the location cannot be copied.

Location can also be copied along with the object in deep copy.

DESTRUCTOR:

Destructor automatically called when an object gets destroyed or this will be called when we exit the main function.

For making destructor function, tilted(~) sign is used followed by class name, here no parameter should be passed and returned.

In this blog, you learn about the beautiful and some important concepts of C++. You learn about the characteristics, like classes and objects, format specifier, member function and so many interesting topics. So that is all for today many more to come, stay connected with us. Keep growing and Happy learning.

ByEE BYee !!

Blog Credits: sh_ruti7679

Post Credits: Vandana Chauhan

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store