0% found this document useful (0 votes)
10 views

C++_Assignment

The document outlines key concepts in C++ programming, including polymorphism (both compile-time and run-time), inline functions, operator overloading, constructor overloading, and call by reference. Each concept is explained with definitions and accompanied by code examples to illustrate their implementation. The document serves as a guide for understanding these fundamental C++ features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C++_Assignment

The document outlines key concepts in C++ programming, including polymorphism (both compile-time and run-time), inline functions, operator overloading, constructor overloading, and call by reference. Each concept is explained with definitions and accompanied by code examples to illustrate their implementation. The document serves as a guide for understanding these fundamental C++ features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Programming Assignment

Name: Dipanshi
Course: ECE, 5th Semester

1. Polymorphism
Polymorphism is the ability of a function, object, or operator to take multiple forms. In C++,
polymorphism can be achieved in two ways:
a. Compile-time Polymorphism (Function Overloading and Operator Overloading)
b. Run-time Polymorphism (Inheritance and Virtual Functions)

Example:

#include <iostream>
using namespace std;

class Shape {
public:
virtual void draw() { // Virtual function
cout << "Drawing a Shape" << endl;
}
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};

int main() {
Shape* shape;
Circle circle;
shape = &circle;
shape->draw(); // Run-time polymorphism
return 0;
}
2. Inline Function
An inline function is a function whose definition is small and can be substituted at the place
of its call to reduce the overhead of a function call.
It is defined using the `inline` keyword.

Example:

#include <iostream>
using namespace std;

inline int square(int x) {


return x * x;
}

int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}

3. Operator Overloading
Operator overloading allows defining custom implementations for operators in user-
defined types. For example, overloading the `+` operator to add two objects.

Example:

#include <iostream>
using namespace std;

class Complex {
private:
float real, imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
Complex operator + (const Complex& c) {
return Complex(real + c.real, imag + c.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(2.5, 3.5), c2(1.5, 2.5);
Complex c3 = c1 + c2;
c3.display();
return 0;
}

4. Constructor Overloading
Constructor overloading allows creating multiple constructors with different parameter
lists in the same class, enabling object initialization in various ways.

Example:

#include <iostream>
using namespace std;

class Rectangle {
private:
int length, width;
public:
Rectangle() : length(0), width(0) {} // Default constructor
Rectangle(int l, int w) : length(l), width(w) {} // Parameterized constructor
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
}
};

int main() {
Rectangle r1; // Calls default constructor
Rectangle r2(10, 20); // Calls parameterized constructor
r1.display();
r2.display();
return 0;
}

5. Call by Reference
Call by reference allows a function to modify the actual arguments passed to it. Instead of
passing values, the memory address of variables is passed.

Example:

#include <iostream>
using namespace std;
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}

int main() {
int a = 10, b = 20;
cout << "Before Swap: a = " << a << ", b = " << b << endl;
swap(a, b);
cout << "After Swap: a = " << a << ", b = " << b << endl;
return 0;
}

You might also like