cpp dps
cpp dps
#include <iostream.h>
class Student
{
public:
int roll_no;
float marks;
};
Void main() {
// Creating an object of the class Student
Student studentObject;
class Triangle {
private:
float side1, side2, side3;
public:
// Constructor to initialize sides of the triangle
Triangle(float s1, float s2, float s3) {
side1 = s1;
side2 = s2;
side3 = s3;
}
// Function to calculate and print the area of the triangle
void calculateArea() {
float s = (side1 + side2 + side3) / 2;
float area = sqrt(s * (s - side1) * (s - side2) * (s - side3));
cout << "Area of the triangle: " << area << " square units" << endl;
}
// Function to calculate and print the perimeter of the triangle
void calculatePerimeter() {
float perimeter = side1 + side2 + side3;
cout << "Perimeter of the triangle: " << perimeter << " units" << endl;
}
};
Void main() {
// Creating a Triangle object with sides 3, 4, and 5 units
Triangle triangle(3, 4, 5);
3.Write a program to print the area of a rectangle by creating a class named 'Area' having two
functions. First function named as 'setDim' takes the length and breadth of the rectangle as
parameters and the second function named as 'getArea' returns the area of the rectangle. Length
and breadth of the rectangle are entered through keyboard.
#include <iostream.h>
class Area {
private:
float length, breadth;
public:
// Function to set the dimensions of the rectangle
void setDim(float len, float brd) {
length = len;
breadth = brd;
}
}
4.Print the average of three numbers entered by the user by creating a class named 'Average'
having a function to calculate and print the average without creating any object of the Average
class.
#include <iostream.h>
class Average {
public:
// Static function to calculate and print the average of three numbers
static void calculateAverage(float num1, float num2, float num3) {
float average = (num1 + num2 + num3) / 3;
cout << "Average of the three numbers: " << average << endl;
}
};
void main() {
float num1, num2, num3;
// Calling the static function of the Average class to calculate and print the average
Average::calculateAverage(num1, num2, num3);
}
1.Create two classes named Mammals and MarineAnimals. Create another class named
BlueWhale which inherits both the above classes. Now, create a function in each of these classes
which prints "I am mammal", "I am a marine animal" and "I belong to both the categories:
Mammals as well as Marine Animals" respectively. Now, create an object for each of the above
class and try calling
i. - function of Mammals by the object of Mammal
ii. - function of MarineAnimal by the object of MarineAnimal
iii. - function of BlueWhale by the object of BlueWhale
iv. - function of each of its parent by the object of BlueWhale
#include <iostream.h>
void main() {
// Creating objects for each class
Mammals mammalObj;
MarineAnimals marineAnimalObj;
BlueWhale blueWhaleObj;
cout << "Calling function of Mammals by the object of BlueWhale:" << endl;
blueWhaleObj.Mammals::display(); // Calling Mammals function using BlueWhale object
cout << "Calling function of MarineAnimals by the object of BlueWhale:" << endl;
blueWhaleObj.MarineAnimals::display(); // Calling MarineAnimals function using BlueWhale
object
}
2. Make a class named Fruit with a data member to calculate the number of fruits in a basket.
Create two other class named Apples and Mangoes to calculate the number of apples and
mangoes in the basket. Print the number of fruits of each type and the total number of fruits in
the basket.
#include <iostream.h>
// Base class Fruit
class Fruit {
protected:
int totalFruits;
public:
Fruit(int total) : totalFruits(total) {}
public:
Mangoes(int total, int mangoes) : Fruit(total), mangoesCount(mangoes) {}
void main() {
int totalFruits = 15; // Total number of fruits in the basket
int applesCount = 8; // Number of apples in the basket
int mangoesCount = 7; // Number of mangoes in the basket
// Displaying the number of fruits of each type and total number of fruits
apples.display();
mangoes.display();
cout << "Total number of fruits in the basket: " << totalFruits << endl;
3.We want to store the information of different vehicles. Create a class named Vehicle
with two data member named mileage and price. Create its two subclasses
*Car with data members to store ownership cost, warranty (by years), seating
capacity and fuel type (diesel or petrol).
*Bike with data members to store the number of cylinders, number of gears,
cooling type(air, liquid or oil), wheel type(alloys or spokes) and fuel tank
size(in inches)
Make another two subclasses Audi and Ford of Car, each having a data member to
store the model type. Next, make two subclasses Bajaj and TVS, each having a data
member to store the make-type.
Now, store and print the information of an Audi and a Ford car (i.e. model type,
ownership cost, warranty, seating capacity, fuel type, mileage and price.) Do the same
for a Bajaj and a TVS bike
#include <iostream.h>
#include <string.h>
// Base class Vehicle
class Vehicle {
protected:
float mileage;
float price;
public:
Vehicle(float m, float p) : mileage(m), price(p) {}
};
// Subclass Car
class Car : public Vehicle {
protected:
float ownershipCost;
int warranty;
int seatingCapacity;
string fuelType;
public:
Car(float m, float p, float oc, int w, int sc, string ft)
: Vehicle(m, p), ownershipCost(oc), warranty(w), seatingCapacity(sc), fuelType(ft) {}
};
// Subclasses of Car
class Audi : public Car {
private:
string modelType;
public:
Audi(float m, float p, float oc, int w, int sc, string ft, string mt)
: Car(m, p, oc, w, sc, ft), modelType(mt) {
}
void displayInfo() {
cout << "Audi Model Type: " << modelType << endl;
cout << "Ownership Cost: $" << ownershipCost << endl;
cout << "Warranty: " << warranty << " years" << endl;
cout << "Seating Capacity: " << seatingCapacity << " persons" << endl;
cout << "Fuel Type: " << fuelType << endl;
cout << "Mileage: " << mileage << " miles/gallon" << endl;
cout << "Price: $" << price << endl;
}
};
public:
Ford(float m, float p, float oc, int w, int sc, string ft, string mt)
: Car(m, p, oc, w, sc, ft), modelType(mt) {
}
void displayInfo() {
cout << "Ford Model Type: " << modelType << endl;
cout << "Ownership Cost: $" << ownershipCost << endl;
cout << "Warranty: " << warranty << " years" << endl;
cout << "Seating Capacity: " << seatingCapacity << " persons" << endl;
cout << "Fuel Type: " << fuelType << endl;
cout << "Mileage: " << mileage << " miles/gallon" << endl;
cout << "Price: $" << price << endl;
}
};
// Subclass Bike
class Bike : public Vehicle {
protected:
int numCylinders;
int numGears;
string coolingType;
string wheelType;
float fuelTankSize;
public:
Bike(float m, float p, int nc, int ng, string ct, string wt, float fts)
: Vehicle(m, p), numCylinders(nc), numGears(ng), coolingType(ct), wheelType(wt),
fuelTankSize(fts) {}
};
// Subclasses of Bike
class Bajaj : public Bike {
private:
string makeType;
public:
Bajaj(float m, float p, int nc, int ng, string ct, string wt, float fts, string mt)
: Bike(m, p, nc, ng, ct, wt, fts), makeType(mt) {}
void displayInfo() {
cout << "Bajaj Make Type: " << makeType << endl;
cout << "Number of Cylinders: " << numCylinders << endl;
cout << "Number of Gears: " << numGears << endl;
cout << "Cooling Type: " << coolingType << endl;
cout << "Wheel Type: " << wheelType << endl;
cout << "Fuel Tank Size: " << fuelTankSize << " inches" << endl;
cout << "Mileage: " << mileage << " miles/gallon" << endl;
cout << "Price: $" << price << endl;
}
};
public:
TVS(float m, float p, int nc, int ng, string ct, string wt, float fts, string mt)
: Bike(m, p, nc, ng, ct, wt, fts), makeType(mt) {}
void displayInfo() {
cout << "TVS Make Type: " << makeType << endl;
cout << "Number of Cylinders: " << numCylinders << endl;
cout << "Number of Gears: " << numGears << endl;
cout << "Cooling Type: " << coolingType << endl;
cout << "Wheel Type: " << wheelType << endl;
cout << "Fuel Tank Size: " << fuelTankSize << " inches" << endl;
cout << "Mileage: " << mileage << " miles/gallon" << endl;
cout << "Price: $" << price << endl;
}
};
int main() {
// Creating objects of Audi, Ford, Bajaj, and TVS
Audi audi(25.5, 30000, 2000, 3, 5, "Petrol", "Audi A4");
Ford ford(22.3, 28000, 1800, 4, 5, "Petrol", "Ford Mustang");
Bajaj bajaj(45.0, 8000, 1, 5, "Air", "Alloys", 10.5, "Pulsar NS160");
TVS tvs(42.7, 9000, 1, 6, "Liquid", "Spokes", 11.2, "Apache RTR 160");
4. Write a C++ program to find Area of square, rectangle, circle and triangle using Function
Overloading.
#include <iostream.h>
#include <cmath.h>
using namespace std;
void main() {
float side, length, width, radius, base, height;
5.Write a C++ program to add two integer numbers, two floating point numbers and one integer
and one floating point number using Function Overloading.
#include <iostream.h>
void main() {
int intNum1, intNum2;
float floatNum1, floatNum2;
cout << "Enter one integer and one floating-point number: ";
cin >> intNum1 >> floatNum1;
cout << "Sum of integer and floating-point number: " << add(intNum1, floatNum1) << endl;
cout << "Enter one floating-point number and one integer: ";
cin >> floatNum1 >> intNum1;
cout << "Sum of floating-point number and integer: " << add(floatNum1, intNum1) << endl;