0% found this document useful (0 votes)
17 views15 pages

Practical File C - Program

Uploaded by

hofficial996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views15 pages

Practical File C - Program

Uploaded by

hofficial996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

C++ Programs for Practical file

NOTE- SUBMISSION DATE IS 24 JUNE 2023

Q1. Write a program to show the concept of Function overloading in C++ using
class and object.

#include <iostream>
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}

double add(double a, double b)


{
return a + b;
}

int add(int a, int b, int c)


{
return a + b + c;
}
};

int main()
{
Calculator calc;
int sum1 = calc.add(3, 4);
double sum2 = calc.add(2.5, 3.7);
int sum3 = calc.add(1, 2, 3);

cout << "Sum 1: " << sum1 << endl;


cout << "Sum 2: " << sum2 << endl;
cout << "Sum 3: " << sum3 << endl;

return 0;
}

OUTPUT-
Sum 1: 7
Sum 2: 6.2
Sum 3: 6

Q2. Write a program to Show the concept of polymorphism.


#include <iostream>
using namespace std;

// Base class
class Shape
{
public:
virtual void draw( )
{
cout << "Drawing a generic shape." << endl;
}
};
// Derived class 1
class Circle : public Shape
{
public:
void draw( ) override
{
cout << "Drawing a circle." << endl;
}
};

// Derived class 2
class Square : public Shape
{
public:
void draw( ) override
{
cout << "Drawing a square." << endl;
}
};

int main()
{
Shape* shapePtr;

Circle circle;
Square square;

shapePtr = &circle;
shapePtr->draw(); // Output: Drawing a circle.
shapePtr = &square;
shapePtr->draw( ); // Output: Drawing a square.

return 0;
}

OUTPUT-

Drawing a circle.
Drawing a square.

Q3. Write a program to explain the concept of constructor in C++.

#include <iostream>

class Rectangle {
private:
double length;
double width;

public:
// Constructor
Rectangle(double len, double wid) {
length = len;
width = wid;
}

// Function to calculate area


double calculateArea() {
return length * width;
}
~ Rectangle();
};

int main() {
// Creating an object of Rectangle class using constructor
Rectangle rectangle(5.0, 3.0);

// Calling the member function to calculate area


double area = rectangle.calculateArea();

cout << "Area of the rectangle: " << area << endl;

return 0;
}

OUTPUT-

Area of the rectangle: 15

Q4. Write a program to show the concept of Single Inheritance.


#include <iostream>
using namespace std;
class Shape // Base class
{
protected:
double width;
double height;
public:
void setDimensions(double w, double h)
{
width = w;
height = h;
}
};
class Rectangle: public Shape // Derived class
{
public:
double calculate Area ( )
{
return width * height;
}
};

int main( )
{
Rectangle rectangle;
rectangle.setDimensions(5.0, 3.0);
double area = rectangle.calculateArea();
cout << "Area of the rectangle: " << area << endl;
return 0;
}

OUTPUT-

Area of the rectangle: 15

Q5. Write a program to show the concept of Multiple Inheritance in C++.

#include <iostream>
using namespace std;

// Base class 1
class Shape
{
protected:
double width;
double height;
public:
void setDimensions(double w, double h) {
width = w;
height = h;
}
};

// Base class 2
class Color {
protected:
string color;
public:
void setColor(string c)
{
color = c;
}
};

// Derived class
class Rectangle : public Shape, public Color
{
public:
void display( )
{
cout << "Width: " << width << endl;
cout << "Height: " << height << endl;
cout << "Color: " << color << endl;
}
};

int main()
{
Rectangle rectangle;
rectangle.setDimensions(5.0, 3.0);
rectangle.setColor("Blue");
rectangle.display();

return 0;
}

OUTPUT-
Width: 5
Height: 3
Color: Blue

Q6. Write a program to show the concept of abstract base class in C++.

#include <iostream>
using namespace std;

// Abstract base class


class Shape
{
public:
virtual double calculateArea() = 0; // Pure virtual function
void printArea( )
{
cout << "Area: " << calculateArea() << endl;
}
};
// Derived class 1
class Circle : public Shape
{
private:
double radius;
public:
Circle(double r) : radius(r)
{
}
double calculateArea() override
{
return 3.14159 * radius * radius;
}
};

// Derived class 2
class Rectangle : public Shape
{
private:
double width;
double height;
public:
Rectangle (double w, double h) : width(w), height(h) {}

double calculateArea() override


{
return width * height;
}
};

int main( )
{
Circle circle(5.0);
circle.printArea();

Rectangle rectangle(4.0, 3.0);


rectangle.printArea();
return 0;
}

OUTPUT-

Area: 78.5397
Area: 12

Q7. Write a program to show the concept of Pure virtual function.


#include <iostream>
using namespace std;

// Abstract base class


class Shape
{
public:
virtual double calculateArea() = 0; // Pure virtual function
};

// Derived class 1
class Circle : public Shape
{
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() override
{
return 3.14159 * radius * radius;
}
};

// Derived class 2
class Rectangle : public Shape
{
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h) {}

double calculateArea() override


{
return width * height;
}
};
int main( )
{
Circle circle(5.0);
cout << "Area of Circle: " << circle.calculateArea() << endl;\
Rectangle rectangle(4.0, 3.0);
cout << "Area of Rectangle: " << rectangle.calculateArea() << endl;

return 0;
}
OUTPUT-
Area of Circle: 78.5397
Area of Rectangle: 12

Q8. Write a program to show the concept of read from a file in C++.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main( )
{
ifstream inputFile("example.txt"); // Open file for reading
string line;

if (inputFile.is_open())
{
while (getline(inputFile, line))
{
cout << line << endl; // Print each line
}
inputFile.close(); // Close the file
} else
{
cout << "Failed to open the file." << endl;
}
return 0;
}
OUTPUT-

Hello, World!
This is a sample file.
Read me line by line.

Q9. Write a program to show the concept of write in a file in C++.

#include <iostream>
#include <fstream>
using namespace std;

int main ( )
{
ofstream outputFile("output.txt"); // Open file for writing

if (outputFile.is_open( ))
{
outputFile << "Hello, World!" << endl;
outputFile << "This is a sample file." << endl;
outputFile << "Writing to a file in C++." << endl;
outputFile.close(); // Close the file
cout << "File written successfully." << endl;
}

else
{
cout << "Failed to open the file." << endl;
}
return 0;
}
OUTPUT-
"File written successfully."

Q10. Write a program to illustrate the concept of Friend class in C++.

#include <iostream>

class FriendClass
{
public:
void display(MyClass & obj)
{
// Accessing private member of MyClass
cout << "Private data in MyClass: " << obj.privateData << endl;
}
};

class MyClass
{
private:
int privateData;
public:
MyClass (int data): privateData(data) {}
friend class FriendClass;
};
int main ( ) {
MyClass obj (42);
FriendClass fc;
fc. display(obj);
return 0;
}
(NOTE –Below mentioned all these questions are available in your google
classroom so check and write it to the file)

Q11. WAP to show the concept of :: ( Scope Resolution Operator) in C++.


Q12. WAP to show the concept of Addition of two numbers using Class.
Q13. WAP to show the concept of operator overloading.
Q14. WAP to show the working of friend function in C++.
Q15. WAP to show the working of inline function.

You might also like