top of page

C plus plus Notes

mobiprep (6).png

Basics of C plus plus

mobiprep (6).png

Basics Of OOPs

mobiprep (6).png

Classes and Objects

mobiprep (6).png

Polymorphism and Inheritence

mobiprep (6).png

Exception Handling

mobiprep (6).png

File Handling and templates

Heading

Q

1

Define class and objects

LRM_EXPORT_207556595493866_20190724_1939

Ans

Class: A class in C++ is the building block, that leads to Object-Oriented programming. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object.

Object: 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.

LRM_EXPORT_207556595493866_20190724_1939

Q

2

Define reference variable. Give its syntax.

LRM_EXPORT_207556595493866_20190724_1939

Ans

When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration.
#include<iostream>
using namespace std;

int main()
{
int x = 10;

// ref is a reference to x.
int& ref = x;

// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;

// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;

return 0;
}

Output:

x = 20
ref =30

LRM_EXPORT_207556595493866_20190724_1939

Q

3

Define instance variables.

LRM_EXPORT_207556595493866_20190724_1939

Ans

"Instance variables are variables defined in a class, but outside the body of methods. The declaration of an instance variable is similar to the declaration of a local variable of a method, but:

the variable is defined inside the class, but outside all methods;
the variable is preceded by an access modifier (usually private);
the variable is always initialized when the object is created, either implicitly (to a default value), or explicitly by the constructor (see later).

Note: This is different from local variables, which are not necessarily initialized when the associated memory location is created.

The instance variables are associated to the single objects and not to the entire class. This means that each object has its own instance variables. Two different objects have different instance variables. "

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Explain different types of constructors.

LRM_EXPORT_207556595493866_20190724_1939

Ans

"A constructor is a special type of member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class because it does not have any return type.

Types of Constructors

1. Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters.

#include <iostream>
using namespace std;

class construct
{
public:
int a, b;

// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << ""a: "" << c.a << endl
<< ""b: "" << c.b;
return 1;
}

Output:

a: 10
b: 20

Note: Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.

2. Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.

#include <iostream>
using namespace std;

class Point
{
private:
int x, y;

public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX()
{
return x;
}
int getY()
{
return y;
}
};

int main()
{
// Constructor called
Point p1(10, 15);

// Access values assigned by constructor
cout << ""p1.x = "" << p1.getX() << "", p1.y = "" << p1.getY();

return 0;
}

Output:

p1.x = 10, p1.y = 15

When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly.

Example e = Example(0, 50); // Explicit call

Example e(0, 50); // Implicit call"

LRM_EXPORT_207556595493866_20190724_1939

Q

5

Explain the use of destructor in C++.

LRM_EXPORT_207556595493866_20190724_1939

Ans

"Destructor is an instance member function which is invoked automatically whenever an object is going to destroy. Means, a destructor is the last function that is going to be called before an object is destroyed.
The thing to be noted is that destructor doesn’t destroys an object.

Syntax:
constructor-name();

Properties of Destructor:

Destructor function is automatically invoked when the objects are destroyed.
It cannot be declared static or const.
The destructor does not have arguments.
It has no return type not even void.
An object of a class with a Destructor cannot become a member of the union.
A destructor should be declared in the public section of the class.
The programmer cannot access the address of destructor."

LRM_EXPORT_207556595493866_20190724_1939
bottom of page