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

Tutorial in C++

Solving some practice Questions
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Tutorial in C++

Solving some practice Questions
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial C++

Kanishk Tomar

23BCE10588

1. Demonstrate Operator Overloading concept in C++ with proper

source code.

// Operator Overloading Example

#include <iostream>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Complex number class

class Complex {

private:

float real;

float imag;

public:

// Constructor

Complex(float r = 0, float i = 0) : real(r), imag(i) {}

// Overload the '+' operator

Complex operator + (const Complex &c) const {

return Complex(real + c.real, imag + c.imag);

// Function to display the complex number

void display() const {

cout << real << " + " << imag << "i" << endl;

};
int main() {

Complex c1(3.5, 2.5), c2(1.5, 4.5);

Complex c3 = c1 + c2; // Calls the overloaded '+' operator

c3.display(); // Outputs: 5.0 + 7.0i

return 0;

2. Discuss the concept of Virtual function , pure virtual function in

Polymorphism . Do justify your answer with suitable code .

In C++, polymorphism allows functions to behave differently based on the object that invokes them.
Virtual functions enable late binding, and pure virtual functions define an interface in a base class
that derived classes must implement.

Virtual Function: A virtual function is a member function in a base class that you expect to override
in derived classes. When you refer to a derived object using a pointer or reference to the base class,
you can call a virtual function to execute the derived class’s version of that function.

Pure Virtual Function: A pure virtual function is a virtual function that has no implementation in the
base class. It is declared by assigning 0 to the function in the base class. A class containing a pure
virtual function is abstract and cannot be instantiated.

#include <iostream>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Base class

class Shape {

public:

// Virtual function

virtual void draw() {

cout << "Drawing a shape" << endl;

// Pure virtual function

virtual void area() = 0; // Must be implemented by derived classes


};

// Derived class 1

class Circle : public Shape {

public:

void draw() override {

cout << "Drawing a circle" << endl;

void area() override {

cout << "Area of circle = πr²" << endl;

};

// Derived class 2

class Rectangle : public Shape {

public:

void draw() override {

cout << "Drawing a rectangle" << endl;

void area() override {

cout << "Area of rectangle = length × width" << endl;

};

int main() {

Shape *s1 = new Circle();

Shape *s2 = new Rectangle();

s1->draw(); // Outputs: Drawing a circle


s1->area(); // Outputs: Area of circle = πr²

s2->draw(); // Outputs: Drawing a rectangle

s2->area(); // Outputs: Area of rectangle = length × width

delete s1;

delete s2;

return 0;

3. Illustrate different types of inheritance in C++ using suitable examples.

Inheritance allows one class to inherit properties and behaviors (methods) from another class. There
are several types of inheritance in C++:

a. Single Inheritance

A derived class inherits from only one base class.

#include <iostream>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Base class

class Animal {

public:

void eat() {

cout << "Eating..." << endl;

};

// Derived class

class Dog : public Animal {

public:
void bark() {

cout << "Barking..." << endl;

};

int main() {

Dog d;

d.eat(); // Inherited from Animal

d.bark(); // Defined in Dog

return 0;

b. Multiple Inheritance

A derived class inherits from more than one base class.

#include <iostream>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Base class 1

class A {

public:

void showA() {

cout << "Class A" << endl;

};

// Base class 2

class B {

public:

void showB() {

cout << "Class B" << endl;


}

};

// Derived class

class C : public A, public B {

public:

void showC() {

cout << "Class C" << endl;

};

int main() {

C c;

c.showA(); // Inherited from A

c.showB(); // Inherited from B

c.showC(); // Defined in C

return 0;

c. Multilevel Inheritance

A derived class inherits from another derived class, forming a chain.

#include <iostream>

using namespace std;

// User: [Your Name], Reg. No.: [Your Registration Number]

// Base class

class A {

public:

void showA() {

cout << "Class A" << endl;

}
};

// Derived class 1

class B : public A {

public:

void showB() {

cout << "Class B" << endl;

};

// Derived class 2

class C : public B {

public:

void showC() {

cout << "Class C" << endl;

};

int main() {

C c;

c.showA(); // Inherited from A

c.showB(); // Inherited from B

c.showC(); // Defined in C

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy