Practical File C - Program
Practical File C - Program
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;
}
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);
return 0;
}
OUTPUT-
Sum 1: 7
Sum 2: 6.2
Sum 3: 6
// 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 = □
shapePtr->draw( ); // Output: Drawing a square.
return 0;
}
OUTPUT-
Drawing a circle.
Drawing a square.
#include <iostream>
class Rectangle {
private:
double length;
double width;
public:
// Constructor
Rectangle(double len, double wid) {
length = len;
width = wid;
}
int main() {
// Creating an object of Rectangle class using constructor
Rectangle rectangle(5.0, 3.0);
cout << "Area of the rectangle: " << area << endl;
return 0;
}
OUTPUT-
int main( )
{
Rectangle rectangle;
rectangle.setDimensions(5.0, 3.0);
double area = rectangle.calculateArea();
cout << "Area of the rectangle: " << area << endl;
return 0;
}
OUTPUT-
#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;
// Derived class 2
class Rectangle : public Shape
{
private:
double width;
double height;
public:
Rectangle (double w, double h) : width(w), height(h) {}
int main( )
{
Circle circle(5.0);
circle.printArea();
OUTPUT-
Area: 78.5397
Area: 12
// 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) {}
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.
#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."
#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)