Constructors: • In C++,constructors are special methods that are automatically called whenever an object of a class is created. The constructor in C++ has the same name as the class or structure. • Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.
3.
Characteristics of constructors: 1. Constructor name and class name should be same. 2. Constructors should be declared in public section. 3. Constructors never have any return value (or) data type including void. 4. The main function of Constructor is to initialize objects and allocate appropriate memory to objects. 5. Constructors can have default values and can be overloaded. 6. Constructor may or may not have arguments (or) parameters. 7. Constructor is executed only once when the object is created. 8. Constructors are automatically invoked they are not invoked with dot(.) operator. 9. Constructor is invoked automatically when object of class is created.
4.
Syntax of defininga constructor in a class class A { public: int x; A() // constructor { // object initialization } };
5.
Example for Creatinga Constructor #include <iostream> using namespace std; class A { public: // Constructor of the class without any parameters A() { cout << "Constructor called" << endl; } }; int main() { A obj1; return 0; }
6.
Types of Constructorsin C++ • C++ supports three types of Constructors 1. Default Constructor. 2. Parameterized Constructor. 3. Copy Constructor.
7.
1. Default Constructor: • Constructor with out arguments are called Default Constructor. • There are two types Default Constructors One is System written which is created by the compiler at compile time(When the class is not having a Constructor. second one, we have to define that is called user defined Constructor.
2. Parameterized Constructor. •In C++, parameterized constructor is a type of constructor that can accept arguments. • Parameterized Constructors make it possible to pass arguments to 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.
3. Copy Constructor. •The process of copying one object data in to another object is called as Copy Constructor. • A copy constructor is a constructor which is used to initialize the current values with another object value. • Copy constructor is used to create another copy or xerox of one object. • Copy constructors are having reference type parameters. • Copy constructors are having class type parameters means object. • Copy constructors receives another object to initialize current object
Constructor Overloading inC++: • In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. • This concept is known as Constructor Overloading and is quite similar to function overloading. • Overloaded constructors essentially have the same name (exact name of the class) and different by number and type of arguments. • A constructor is called depending upon the number and type of arguments passed. • While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.
Constructors with defaultarguments • In C++, constructors can have default arguments, which allow you to create objects without passing all parameters explicitly.
Dynamic Constructor • InC++, a "dynamic constructor" is a term used to describe a constructor that performs dynamic memory allocation during the creation of an object. • This means the constructor uses the new operator to allocate memory on the heap for one or more of the object's data members. Why Use Dynamic Constructors? • Dynamic constructors can optimize memory usage, particularly when you need to declare objects with variable sizes. • With dynamic constructors, memory allocation for an object occurs only if it is required, preventing unnecessary memory wastage.
Destructors • Destructor isan instance member function that is invoked automatically whenever an object is going to be destroyed. • Meaning, a destructor is the last function that is going to be called before an object is destroyed. Syntax: • Destructors are automatically present in every C++ class but we can also redefine them using the following syntax. ~className(){ // Body of destructor } • where, tilda(~) is used to create destructor of a className.
23.
Characteristics of constructors: • Destructor has the same name as their class name preceded by a tilde (~) symbol. • It is not possible to define more than one destructor. • The destructor is only one way to destroy the object created by the constructor. Hence, destructor cannot be overloaded. • It cannot be declared static or const. • Destructor neither requires any argument nor returns any value. • It is automatically called when an object goes out of scope. • Destructor release memory space occupied by the objects created by the constructor. • In destructor, objects are destroyed in the reverse of an object creation.
Operator Overloading inC++ • Operator overloading in C++ is a compile-time polymorphism feature that allows operators to be redefined or given special meaning when used with user- defined data types (classes). • It enables operators to work with objects of custom classes in a natural and intuitive way, similar to how they work with built-in data types.
28.
Why use OperatorOverloading? • Operators like +, -, *, etc., are already defined for built-in types such as int, float, double, etc. • But when working with user-defined types (like class Complex, class Fraction, or class BigInteger), these operators don’t work out of the box
29.
Operator overloading • Whenan operator is overloaded, the produced symbol called operator function name. • Operator function should be either member function or friend function. • Friend function requires one argument for unary operator and two for binary operators. • Member function requires one arguments for binary operators and zero arguments for unary operators.
30.
Goal of OperatorOverloading • To redefine the behavior of an operator so that it works with objects of a user-defined type, while still retaining its meaning for built-in types. • Note: Overloading doesn't replace existing functionality — it extends it for user-defined types. • Syntax of Operator Overloading return_type operator op (arguments) { // function body }
31.
Operators that Canand Cannot be Overloaded Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that cannot be overloaded . .* :: ?:
32.
Operator Overloading • Touse any operators on a class object, … • The operator must be overloaded for that class. • Three Exceptions: {overloading not required} • Assignment operator (=) • Member wise assignment between objects • Dangerous for classes with pointer members!! • Address operator (&) • Returns address of the object in memory. • Comma operator (,) • Evaluates expression to its left then the expression to its right. • Returns the value of the expression to its right.(e.g (t1, t2).show(); )
Rules for overloadingof operators In C++, following are the general rules for the things that are not allowed with operator overloading. 1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. (arity is the number of arguments or operands taken by a function or operation) 3) Precedence and associativity of the operators cannot be changed. 4) Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.
37.
Rules for overloadingof operators 5) Operators cannot be overloaded for built in types only. At least one operand must be defined type. 6) Assignment (=), subscript ([]), function call (“()”), and member selection (->) operators must be defined as member functions 7) Except the operators specified in point 6, all other operators can be either member functions or a non member functions. 8 ) Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
38.
General rules ofthe operator overloading • Syntax/Declaration Rule: While overloading a function we need to use the keyword operator , proceeded by class name and followed by operator symbol such as '+’. • Member and Non-member functions: We can overload operators in two ways:- first is as a member functions , second is as a non-member functions. When overloading as a member function, the left operand represents the object on which the function is called and when overloading as a non-member function, we can specify any type for the left operand. • Number of operands Required: Most operators can be overloaded with one or two operands. Example- Unary operators like ++ require one operand, while binary operators like + require two operands.
39.
General rules ofthe operator overloading • Precedence and associativity: Operator precedence and associativity cannot be changed when overloading operators. Example- It's not allowed to change the precedence of (*) or the associativity of (/). • Return type: When we overload operators than we must define the return type of the overloaded operator function according to our need. • Friend function: If overloaded operators needs to access private member variables/functions of a class than they must be declared as a friend function. • Special Syntax: For operators like (), [], or -> we must use a special syntax when overloading. Example, in order to overload the [] operator and to make objects callable like functions, we need to define a function named operator().