A parameter with a default value, is often known as an "optional parameter".
Multiple Parameters
When you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
void myFunction(string fname, int age){ cout << "Hello! " << fname << ". You are " << age << endl;}int main(){ myFunction("Dipesh", 19); myFunction("Nilesh", 20); return 0;}
Hello! Dipesh. You are 19Hello! Nilesh. You are 20
Call by Value
In call by value, original value is not modified.
The function receives a copy of the actual parameter values passed to it
Any changes made to the parameters inside the function do not affect the original variables in the calling function
The function works with its own local copies of the variables
#include <iostream>using namespace std;// Function that swaps two integersvoid swapByValue(int x, int y) { int temp = x; x = y; y = temp;}int main() { int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; // Call the swapByValue function swapByValue(a, b); cout << "After swap: a = " << a << ", b = " << b << endl; return 0;}
Before swap: a = 5, b = 10After swap: a = 5, b = 10
Call by Reference
In this method function receives a reference to the actual variable rather than a copy of its value
function can directly modify the original variable's value
Any changes made inside the function are reflected in the variable in the calling function.
#include <iostream>using namespace std;// Function that swaps two integers using call by referencevoid swapByReference(int &x, int &y) { int temp = x; x = y; y = temp;}int main() { int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; // Call the swapByReference function swapByReference(a, b); cout << "After swap: a = " << a << ", b = " << b << endl; return 0;}
Before swap: a = 5, b = 10After swap: a = 10, b = 5
Inline Function
An inline function is a function that is expanded in line when it is called
When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call.
An inline function may increase efficiency if it is small.
Syntax
inline return-type function-name(parameters){ // function code}
Inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining.
#include <iostream>using namespace std;inline int cube(int s) { return s * s * s; }int main(){ cout << "The cube of 3 is: " << cube(3) << "\n"; return 0;}
Static Data Member
The static keyword is used with a variable to make the memory of the variable static once a static variable is declared its memory can’t be changed
Static members of a class are not associated with the objects of the class
Once a static member is declared it will be treated as same for all the objects associated with the class
Static Member Function in a class is the function that is declared as static because of which function attains certain properties:
A static member function can be called even if no objects of the class exist
A static member function can also be accessed using the class name through the scope resolution operator.
A static member function can access static data members and static member functions inside or outside of the class
Static member functions have a scope inside the class and cannot access the current object pointer
You can also use a static member function to determine how many objects of the class have been created
#include <iostream>using namespace std;class Box { private: static int length; static int breadth; static int height; public: static void print(){ cout << "The value of the length is: " << length << endl; cout << "The value of the breadth is: " << breadth << endl; cout << "The value of the height is: " << height << endl; }};// initialize the static data membersint Box :: length = 10;int Box :: breadth = 20;int Box :: height = 30;int main(){ Box b; cout << "Static member function is called through Object name: \n" << endl; b.print(); cout << "\nStatic member function is called through Class name: \n" << endl; Box::print(); return 0;}
Static member function is called through Object name: The value of the length is: 10 The value of the breadth is: 20 The value of the height is: 30Static member function is called through Class name: The value of the length is: 10 The value of the breadth is: 20 The value of the height is: 30
Why we need Static Member Function
Static members are frequently used to store information that is shared by all objects in a class.
For instance, you may keep track of the quantity of newly generated objects of a specific class type using a static data member as a counter. This static data member can be increased each time an object is generated to keep track of the overall number of objects.
Friend Function
A Friend Function is a function that can access and use the protected and private data of a class
By using the keyword friend compiler knows the given function is a friend function
For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend
Syntax
class class_name{ friend data_type function_name(argument/s);};
Characteristics of Friend Function
The function is not in the scope of the class to which it has been declared as a friend
It cannot be called using the object as it is not in the scope of that class
It can be invoked like a normal function without using the object.
It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
It can be declared either in the private or the public part.
A constructor without any arguments or with the default value for every argument is said to be the Default constructor
A constructor that accept no arguments is called a zero argument constructor or default constructor.
If default constructor is not defined in the source code by the programmer, then the compiler defined the default constructor implicitly during compilation.
They are used to create objects, which do not have any specific initial value.
#include <iostream>using namespace std;class A{ public: // User defined constructor A(){ cout << "A Constructor" << endl; } // uninitialized int size;};class B : public A{ // compiler defines default constructor of B, and //inserts stub to call A constructor // compiler won't initialize any data of A};class C : public A { public: C(){ // User defined default constructor of C // Compiler inserts stub to call A's constructor cout << "C Constructor" << endl; // compiler won't initialize any data of A }};class D { public: D() { // User defined default constructor of D // a - constructor to be called, compiler inserts // stub to call A constructor cout << "D Constructor" << endl; // compiler won't initialize any data of 'a' } private: A a;};int main() { Base base; B b; C c; D d; return 0;}
Parameterized Constructor
A parameterized constructor in C++ is a constructor that accepts parameters during object initialization.
It allows you to initialize the object's data members with specific values at the time of object creation.
This is particularly useful when you want to create objects with different initial states.
There is a minute difference between default constructor and Parametrized constructor.
The default constructor is a type of constructor which has no arguments but yes object instantiation is performed there also.
Parametrized constructor is a special type of constructor where an object is created, and further parameters are passed to distinct objects.
#include <iostream>using namespace std;class Rectangle {private: int length; int width;public: // Parameterized constructor Rectangle(int l, int w) { length = l; width = w; } // Function to calculate area int area() { return length * width; }};int main() { // Create Rectangle object with width 5 and length 10 Rectangle rect1(10, 5); // Calculate and print area of rect1 cout << "Area of rectangle 1: " << rect1.area() << endl; // Create Rectangle object with width 7 and length 3 Rectangle rect2(3, 7); // Calculate and print area of rect2 cout << "Area of rectangle 2: " << rect2.area() << endl; return 0;}
Copy Constructor
A copy constructor in C++ is a special constructor used to create a new object as a copy of an existing object of the same class.
It is called when an object is initialized with another object of the same class, either by direct initialization, passing by value, or returning by value.
In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor
Copy constructor takes a reference to an object of the same class as an argument.
The process of initializing members of an object through a copy constructor is known as copy initialization.
It is also called member-wise initialization because the copy constructor initializes one object with the existing object, both belonging to the same class on a member by member copy basis.
Characteristics of Copy Constrictor
The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object.
The process of initializing members of an object through a copy constructor is known as copy initialization
It is also called member-wise initialization because the copy constructor initializes one object with the existing object, both belonging to the same class on a member-by-member copy basis
Types of Copy Constructor
Default Copy Constructor
User Defined Copy Constructor
Default Copy Constructor
The default copy constructor performs a member-wise copy of each data member from the source object to the destination object.
An implicitly defined copy constructor will copy the bases and members of an object in the same order that a constructor would initialize the bases and members of the object.
A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written
In C++, a Copy Constructor may be called in the following cases:
When an object of the class is returned by value.
When an object of the class is passed (to a function) by value as an argument.
When an object is constructed based on another object of the same class.
Multiple Constructor
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.
#include <iostream>using namespace std;class construct{ public: float area; // Constructor with no parameters construct(){ area = 0; } // Constructor with two parameters construct(int a, int b){ area = a * b; } void disp(){ cout<< area<< endl; }};int main(){ // Constructor Overloading // with two different constructors // of class name construct o; construct o2( 10, 20); o.disp(); o2.disp(); return 1;}
0200
Destructors
Destructor is an 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.
Destructor destroys the class objects created by the constructor.
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 can-not be overloaded.
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.
Syntax
//The syntax for defining the destructor within the class:~ <class-name>() { // some instructions}
//The syntax for defining the destructor outside the class:<class-name> :: ~<class-name>() { // some instructions}