Understanding Constructors and Destructors in C++
Constructors and Destructors are executed in C++? A constructor is a member function with the same name as the class. It is called automatically when an object is created. Destructors have the same class name preceded by a tilde symbol. They remove and destroy the memory allocated by the constructor. A constructor initializes the object and allocates memory for it. A class can have multiple constructors but only one destructor. Syntax: The syntax of the constructor in C++ is given below. In the above syntax, class_name is the constructor’s name, public is an access specifier, and the parameter list is optional. Syntax: The syntax of the destructor in C++ is given below. Here, we use the tilde symbol for defining the destructor in C++ programming. The destructor has no argument and does not return any value. #include <iostream.h> #include<conio.h> class MyClass { public: int x; MyClass(); ~MyClass(); }; MyClass::MyClass() { x = 10; } MyClass::~MyClass() { cout<< “Destructing …\n”; } void main() { clrscr(); MyClass ob1; MyClass ob2; cout ob1.x<<” “<<ob2.x<<“\n”; getch(); } output: 10,10 Scope Resolution Operator in C++ #include <iostream> using namespace std; class Rectangle { private: int length; int breadth; public: Rectangle (int l, int b) { length = 1; breadth = b; } int area () { return length * breadth; } int perimeter (); }; int Rectangle::perimeter() { return 2*(length + breadth); } int main() { Rectangle r(8, 3); cout << “Area is :”<< r.area() <<endl; cout << “perimeter is “<< r.perimeter(); } output: area 24, perimeter=22 GENERAL FORM OF C++ PROGRAM Although individual styles will differ, most C++ programs will have this general form: #includes base-class declarations derived class declarations nonmember function prototypes int main( ) { //.. } nonmember function definitions In most large projects, all class declarations will be put into a header file and included with each module. But the general organization of a program remains the same. Example :- # include <iostream.h> class person { char name[30]; int age ; public: void getdata( ); void display( ); }; void person :: getdata { cout<<“enter name”; cin>>name; cout<<“enter age”; cin>>age; } void display() { cout<<“\n name:”<<name; cout<<“\n age:”<<age; } int main( ) { person p; p.getdata(); p.display(); return 0; } define OOPs and its key concepts? Object-oriented programming (OOP) is a programming paradigm that uses objects to design applications and computer programs. It utilizes key concepts like Classes & Objects, Encapsulation, Inheritance, and Polymorphism. Advantages of OOPs: Data security, Reusability of existing code, Creating new data types, Abstraction, Less development time, Reduce complexity, Better productivity. Class and objects relationship: Classes are created using the keyword class. A class declaration defines a new type that links code and data. General form of class declaration is: class class_name { access specifier: data access specifier: functions; }; Classes in C++: Class in C++ is the building block that leads to Object-Oriented programming. It is a user-defined data type that holds its own data members and member functions. A C++ class is like a blueprint for an 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. Defining Class: A class is defined in C++ using the keyword class followed by the name of the class. Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects. Accessing member functions: The data members and member functions of the class can be accessed using the dot operator with the object. Accessing Data Members: The public data members are accessed in the same way, however, the private data members are not allowed to be accessed directly by the object. Function Overloading and Ambiguity: Function overloading in C++ allows you to define multiple functions with the same name but different parameter lists. However, there are situations where overloading can lead to ambiguity, making it unclear for the compiler which function to choose when a particular function is called. Advantages: Memory space is saved, Different behavior with the same function name, Fast execution, Code reusability, Easy maintenance and debugging. Operator Overloading: Operator overloading is a way to give special meaning to an existing operator in C++ without changing its original meaning. It allows operators to be redefined so that they work for user-defined data types. Binary Operator Syntax as a member function: return-type operator < symbol > (one input argument) { } If a binary operator is overloaded using a member function, one argument is needed. Syntax as a Friend function: friend return-type operator < symbol > (two input arguments) { } If a binary operator is overloaded using a friend function, two arguments are needed. Operator Overloading in C++ Using Friend Function: #include <iostream.h> using namespace std; class Complex { private: int real; int img; public: Complex (int r = 0, int i = 0) { real = r; img = i; } void Display () { cout << real << “+i” << img; } friend Complex operator + (Complex c1, Complex c2); }; Complex operator + (Complex c1, Complex c2) { Complex temp; temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; return temp; } int main () { Complex C1(5, 3), C2(10, 5), C3; C1.Display(); cout << ” + “; C2.Display(); cout << ” = “; C3=C1+C2; C3 .Display (); } Overloading New and Delete Operator: The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or for specific classes. If these operators are overloaded using a member function for a class, it means that these operators are overloaded only for that specific class. If overloading is done outside a class, the overloaded new and delete will be called anytime you make use of these operators. Inheritance: It’s a method of implementing reusability of classes. The members of one class can be accumulated in another class through inheritance. The class that provides its members for inheritance is called the Parent or Base Class, and the class that acquires the members is called the Child or Derived Class. There are three main types of inheritance: Public Inheritance, Private Inheritance, and Protected Inheritance. There are five types of inheritance structures: Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance, Hierarchical Inheritance, and Hybrid Inheritance. Constructors and Destructors and Inheritance: When inheritance takes place, an unnamed instance of the parent class is created for every object created from the child class. The constructor of the parent class is invoked when the instance is created. The destructors are called in the reverse order of the constructors. Virtual Base classes: A virtual base class is a class that serves as a common base class for multiple derived classes. When a base class is marked as virtual, it helps in avoiding issues related to multiple inheritance. Virtual functions are Hierarchical: Virtual functions can be part of a hierarchy of classes in C++. This hierarchy is created through inheritance, allowing for polymorphism where a base class pointer or reference can refer to objects of derived classes, and the correct function is called based on the actual type of the object at runtime. Pure virtual functions: Pure virtual functions are functions that have no implementation in the base class and must be implemented in the derived classes. They are used to achieve runtime polymorphism. Early Binding: Early binding is compile-time polymorphism where the function call is associated directly by the compiler. Late Binding: Late binding is runtime polymorphism where the function call is decided at runtime based on the object’s type.
