Classes and Objects: C plus plus Class Notes

Updated: Aug 18

Mobiprep has created last-minute notes for all C++ and templates to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on C++.

  1. Basics of C plus plus

  2. Basics Of OOPs

  3. Classes and Objects

  4. Polymorphism and Inheritence

  5. Exception Handling

  6. File Handling and templates

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. Define class and objects

  2. Define reference variable. Give its syntax.

  3. Define instance variables.

  4. Explain different types of constructors.

  5. Explain the use of destructor in C++.

Classes and Objects


Question 1) Define class and objects.

Answer) 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.


 

Question 2) Define reference variable. Give its syntax.

Answer) 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

 

Question 3) Define instance variables.

Answer) "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. "


 

Question 4) Explain different types of constructors.

Answer) "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"


 

Question 5) Explain the use of destructor in C++.

Answer) "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."





5 views0 comments

Recent Posts

See All