C++ PYQ Programs
C++ PYQ Programs
int main() {
// Single Inheritance
cout << "Single Inheritance Example:" << endl;
Dog dog;
dog.eat(); // From Animal class
dog.bark(); // From Dog class
cout << endl;
// Multiple Inheritance
cout << "Multiple Inheritance Example:" << endl;
Car car;
car.drive(); // From Vehicle class
car.operate(); // From Machine class
car.honk(); // From Car class
cout << endl;
// Multilevel Inheritance
cout << "Multilevel Inheritance Example:" << endl;
ColoredRectangle coloredRect;
coloredRect.draw(); // From Shape class
coloredRect.area(); // From Rectangle class
coloredRect.color(); // From ColoredRectangle class
return 0;
}
class MyClass {
private:
int value;
public:
// Constructor to initialize the value
MyClass(int v) : value(v) {}
int main() {
MyClass obj1(10);
MyClass obj2(20);
return 0;
}
Q. Write a program for dynamic memory allocation.
#include <iostream>
using namespace std;
int main() {
// Dynamically allocate memory for an integer
int* ptr = new int;
return 0;
}
// Base class
class Base {
public:
Base() {
cout << "Base class constructor called!" << endl;
}
};
// Derived class
class Derived : public Base {
public:
Derived() {
cout << "Derived class constructor called!" << endl;
} };
int main() {
// Creating an object of the derived class
Derived obj;
return 0;
}
Q. Wap to overload unary addition operator
#include <iostream>
using namespace std;
class MyClass {
private:
int value;
public:
// Constructor to initialize the value
MyClass(int v) : value(v) {}
int main() {
MyClass obj1(10);
MyClass obj2(20);
return 0;
}
class MyClass {
private:
int value;
public:
// Constructor to initialize value
MyClass(int v) : value(v) {}
// Declaring friend function
friend void displayValue(const MyClass& obj);
};
int main() {
MyClass obj(42); // Create an object of MyClass with value 42
displayValue(obj);
return 0;
}
C ->>
#include <stdio.h>
#include <conio.h>
void main(){
printf("Hello Duniya !!!! ");
}
C++ ->>
#include <iostream>
void main(){
cout<<"Hello Duniya!!!! ";
}
class Factorial {
private:
int number;
int fact;
public:
// Constructor to initialize the number and calculate the factorial
Factorial(int num) {
number = num;
fact = 1;
calculateFactorial();
}
};
int main() {
int num;
int main() {
// Create objects of the derived classes
Dog dog;
Cat cat;
Cow cow;
return 0;
}
int main() {
// Declare variables
const int NUM_STUDENTS = 50;
float marks[NUM_STUDENTS];
float sum = 0.0;
float average;
return 0;
}
Q. Write a sample code to show the use of constructor
#include <iostream>
using namespace std;
// Base class
class Base {
public:
Base() {
cout << "Base class constructor called!" << endl;
}
};
// Derived class
class Derived : public Base {
public:
Derived() {
cout << "Derived class constructor called!" << endl;
} };
int main() {
// Creating an object of the derived class
Derived obj;
return 0;
}
int main() {
// Create objects of the derived classes
Dog dog;
Cat cat;
Cow cow;
return 0;
}
// Function that divides two numbers and throws an exception if division by zero occurs
double divide(double numerator, double denominator) {
if (denominator == 0) {
// Throwing exception when division by zero is attempted
throw "Error: Division by zero!"; }
return numerator / denominator; }
int main() {
double num1, num2;
return 0; }
Q. WAP to update the content using random access.
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
struct Record {
int id;
char name[50];
};
int main() {
string filename = "data.txt";
vector<Record> records;
Record record;
while (file >> record.id) {
file.ignore();
file.getline(record.name, 50);
records.push_back(record);
}
file.close();
return 0; }
Q.WAP to check palindrome
#include <iostream>
using namespace std;
int main() {
int num;
// Input the number
cout << "Enter a number: ";
cin >> num;
int main() {
string str1, str2;
return 0; }
Q. Wap to print following pattern
#include <iostream>
using namespace std;
int main() {
int main(){
int num;
cout<<"Enter the Number";
cin>>num;
int sum = 0;
for (int i = 0; i <= num; i++)
{
sum+=i;
}
int main() {
double length, width, base, height, radius;
// Rectangle
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
cout << "Area of Rectangle: " << calculateArea(length, width) << endl;
// Triangle
cout << "Enter the base and height of the triangle: ";
cin >> base >> height;
cout << "Area of Triangle: " << calculateArea(base, height, 0) << endl;
// Sphere
cout << "Enter the radius of the sphere: ";
cin >> radius;
cout << "Surface Area of Sphere: " << calculateArea(radius) << endl;
return 0;
}
Q. Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int num1 = 0, num2 = 1, nextTerm;
cout << "Fibonacci Series: ";
class Train {
private:
int trainNumber;
string trainName;
string source;
string destination;
int capacity;
public:
// Function to initialize train data
void initializeMember(int tNumber, string tName, string src, string dest, int
cap) {
trainNumber = tNumber;
trainName = tName;
source = src;
destination = dest;
capacity = cap;
}
int main() {
Train train;
// Option to initialize or input train data
int choice;
cout << "Choose an option: \n1. Initialize Train Data\n2. Input Train Data\nEnter
your choice: ";
cin >> choice;
if (choice == 1) {
// Initialize train data using default values
train.initializeMember(12345, "Express Train", "City A", "City B", 500);
} else
if (choice == 2) {
// Input train data from the user
train.inputTrainData();
} else {
cout << "Invalid choice!" << endl;
return 1;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Name of the file to store data
string fileName = "telephone_list.txt";
cout << "Data has been written to " << fileName << " successfully!" << endl;
return 0;
}