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

cpp dps

Uploaded by

dgpguru
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)
23 views

cpp dps

Uploaded by

dgpguru
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/ 10

1.

Write a program in C++ to Create a class named 'Student' with an


integer variable 'roll_no' and float variable ‘marks’ . Assign the value of
roll_no as '2' and that of marks as ‘80’ by creating an object of the class
Student

#include <iostream.h>
class Student
{
public:
int roll_no;
float marks;
};

Void main() {
// Creating an object of the class Student
Student studentObject;

// Assigning values to the member variables of the object


studentObject.roll_no = 2;
studentObject.marks = 80.0;

// Displaying the values


cout << "Roll Number: " << studentObject.roll_no << endl;
cout << "Marks: " << studentObject.marks << endl;

2.Write a program to print the area and perimeter of a triangle having


sides of 3, 4 and 5 units by creating a class named 'Triangle' with a
function to print the area and perimeter.
#include <iostream.h>
#include <cmath.h>
using namespace std;

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);

// Calculating and printing area and perimeter of the triangle


triangle.calculateArea();
triangle.calculatePerimeter();
}

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;
}

// Function to calculate and return the area of the rectangle


float getArea() {
return length * breadth;
}
};
void main() {
float length, breadth;

// Creating an object of the class Area


Area rectangle;

// Taking length and breadth as input from the user


cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter breadth of the rectangle: ";
cin >> breadth;

// Setting the dimensions of the rectangle


rectangle.setDim(length, breadth);

// Calculating and printing the area of the rectangle


float area = rectangle.getArea();
cout << "Area of the rectangle: " << area << " square units" << endl;

}
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;

// Taking three numbers as input from the user


cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Enter third number: ";
cin >> num3;

// Calling the static function of the Average class to calculate and print the average
Average::calculateAverage(num1, num2, num3);
}

Important Problems from Inheritance and Function Overloading

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>

// Base class Mammals


class Mammals {
public:
void display() {
cout << "I am a mammal" << endl;
}
};

// Base class MarineAnimals


class MarineAnimals {
public:
void display() {
cout << "I am a marine animal" << endl;
}
};

// Derived class BlueWhale inheriting from Mammals and MarineAnimals


class BlueWhale : public Mammals, public MarineAnimals {
public:
void display() {
cout << "I belong to both the categories: Mammals as well as Marine Animals" << endl;
}
};

void main() {
// Creating objects for each class
Mammals mammalObj;
MarineAnimals marineAnimalObj;
BlueWhale blueWhaleObj;

// Calling functions using objects of respective classes


cout << "Calling function of Mammals by the object of Mammals:" << endl;
mammalObj.display();
cout << "Calling function of MarineAnimals by the object of MarineAnimals:" << endl;
marineAnimalObj.display();
cout << "Calling function of BlueWhale by the object of BlueWhale:" << endl;
blueWhaleObj.display();

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) {}

// Function to display the number of fruits of a specific type


virtual void display() {
cout << "Number of fruits: " << totalFruits << endl;
}
};

// Derived class Apples


class Apples : public Fruit {
private:
int applesCount;
public:
Apples(int total, int apples) : Fruit(total), applesCount(apples) {}

// Function to display the number of apples


void display() override {
cout << "Number of apples: " << applesCount << endl;
}
};

// Derived class Mangoes


class Mangoes : public Fruit {
private:
int mangoesCount;

public:
Mangoes(int total, int mangoes) : Fruit(total), mangoesCount(mangoes) {}

// Function to display the number of mangoes


void display() override {
cout << "Number of mangoes: " << mangoesCount << endl;
}
};

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

// Creating objects of Apples and Mangoes classes


Apples apples(totalFruits, applesCount);
Mangoes mangoes(totalFruits, mangoesCount);

// 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;
}
};

class Ford : public Car {


private:
string modelType;

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;
}
};

class TVS : public Bike {


private:
string makeType;

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");

// Displaying information of Audi and Ford cars


cout << "Information of Audi Car:" << endl;
audi.displayInfo();
cout << endl;
cout << "Information of Ford Car:" << endl;
ford.displayInfo();
cout << endl;

// Displaying information of Bajaj and TVS bikes


cout << "Information of Bajaj Bike:" << endl;
bajaj.displayInfo();
cout << endl;
cout << "Information of TVS Bike:" << endl;
tvs.displayInfo();
cout << endl;
}

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;

const float PI = 3.14159265358979323846;

// Function to calculate the area of a square


float calculateArea(float side) {
return side * side;
}

// Function to calculate the area of a rectangle


float calculateArea(float length, float width) {
return length * width;
}

// Function to calculate the area of a circle


float calculateArea(float radius, bool isCircle = true) {
if (isCircle) {
return PI * radius * radius;
} else {
cout << "Invalid option for calculateArea function." << endl;
return -1;
}
}

// Function to calculate the area of a triangle


float calculateArea(float base, float height, bool isTriangle = true) {
if (isTriangle) {
return 0.5 * base * height;
} else {
cout << "Invalid option for calculateArea function." << endl;
return -1;
}
}

void main() {
float side, length, width, radius, base, height;

cout << "Enter side of the square: ";


cin >> side;
cout << "Area of the square: " << calculateArea(side) << endl;

cout << "Enter length and width of the rectangle: ";


cin >> length >> width;
cout << "Area of the rectangle: " << calculateArea(length, width) << endl;

cout << "Enter radius of the circle: ";


cin >> radius;
cout << "Area of the circle: " << calculateArea(radius) << endl;

cout << "Enter base and height of the triangle: ";


cin >> base >> height;
cout << "Area of the triangle: " << calculateArea(base, height) << endl;

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>

// Function to add two integers


int add(int num1, int num2) {
return num1 + num2;
}

// Function to add two floating-point numbers


float add(float num1, float num2) {
return num1 + num2;
}

// Function to add one integer and one floating-point number


float add(int num1, float num2) {
return num1 + num2;
}
// Function to add one floating-point number and one integer
float add(float num1, int num2) {
return num1 + num2;
}

void main() {
int intNum1, intNum2;
float floatNum1, floatNum2;

cout << "Enter two integer numbers: ";


cin >> intNum1 >> intNum2;
cout << "Sum of integers: " << add(intNum1, intNum2) << endl;

cout << "Enter two floating-point numbers: ";


cin >> floatNum1 >> floatNum2;
cout << "Sum of floating-point numbers: " << add(floatNum1, floatNum2) << endl;

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;

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