C plus plus Notes
Basics of C plus plus
Basics Of OOPs
Classes and Objects
Polymorphism and Inheritence
File Handling and templates
Define class and objects
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.
Define reference variable. Give its syntax.
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.
using namespace std;
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 ;
x = 20
Define instance variables.
"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. "
Explain different types of constructors.
"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.
using namespace std;
int a, b;
// Default Constructor
a = 10;
b = 20;
// Default constructor called automatically
// when the object is created
cout << ""a: "" << c.a << endl
<< ""b: "" << c.b;
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.
using namespace std;
int x, y;
// Parameterized Constructor
Point(int x1, int y1)
x = x1;
y = y1;
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << ""p1.x = "" << p1.getX() << "", p1.y = "" << p1.getY();
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"
Explain the use of destructor in C++.
"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.
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."