Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1.

#include <iostream>
#include <string>

class Person {
private:
std::string name;
int age;

public:
// Constructor to initialize the class members
Person(std::string n, int a) : name(n), age(a) {}

// Member function to display the contents of the class members


void display() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};

int main() {
// Create an object of the Person class and initialize it
Person person("John Doe", 30);

// Display the contents of the object


person.display();

return 0;
}

2.

#include <iostream>
#include <string>

class Student {
private:
std::string name;
int rollNo;
char grade;

public:
// Constructor to initialize the class members
Student() : name(""), rollNo(0), grade('F') {}

// Member function to read the details of the student


void readDetails() {
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Enter roll number: ";
std::cin >> rollNo;
std::cout << "Enter grade: ";
std::cin >> grade;
}

// Member function to display the details of the student


void displayDetails() const {
std::cout << "Name: " << name << ", Roll No: " << rollNo << ", Grade: " << grade << std::endl;
}
};

int main() {
const int numberOfStudents = 3;
Student students[numberOfStudents];

// Read details for each student


for (int i = 0; i < numberOfStudents; ++i) {
std::cout << "Enter details for student " << i + 1 << ":\n";
students[i].readDetails();
}

// Display details for each student


std::cout << "\nStudent Details:\n";
for (int i = 0; i < numberOfStudents; ++i) {
students[i].displayDetails();
}

return 0;
}

3.

#include <iostream>

// Global variable
int value = 10;

class Example {
public:
// Class member variable
static int value;

// Function to display values from different scopes


void displayValues(int value) {
// Local variable (parameter)
std::cout << "Local variable value: " << value << std::endl;
// Class member variable
std::cout << "Class member variable value: " << Example::value << std::endl;
// Global variable
std::cout << "Global variable value: " << ::value << std::endl;
}
};

// Definition of the class member variable


int Example::value = 20;

int main() {
// Local variable in main
int value = 30;

Example ex;
ex.displayValues(value);
return 0;
}

4.

#include <iostream>
#include <cmath>

// Function to calculate the area of a circle


double calculateArea(double radius) {
return M_PI * radius * radius;
}

// Function to calculate the area of a rectangle


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

// Function to calculate the area of a square


double calculateArea(int side) {
return side * side;
}

// Function to calculate the area of a triangle


double calculateArea(double base, double height, bool isTriangle) {
if (isTriangle) {
return 0.5 * base * height;
}
return -1; // This case will never occur, it's just a safeguard.
}

int main() {
// Circle
double radius = 5.0;
std::cout << "Area of Circle (radius " << radius << "): " << calculateArea(radius) << std::endl;

// Rectangle
double length = 10.0;
double width = 5.0;
std::cout << "Area of Rectangle (length " << length << ", width " << width << "): " <<
calculateArea(length, width) << std::endl;

// Square
int side = 4;
std::cout << "Area of Square (side " << side << "): " << calculateArea(side) << std::endl;

// Triangle
double base = 6.0;
double height = 3.0;
std::cout << "Area of Triangle (base " << base << ", height " << height << "): " << calculateArea(base,
height, true) << std::endl;

return 0;
}

5.
#include <iostream>
class Arithmetic {
public:
// Inline function for addition
inline int add(int a, int b) {
return a + b;
}

// Inline function for subtraction


inline int subtract(int a, int b) {
return a - b;
}

// Inline function for multiplication


inline int multiply(int a, int b) {
return a * b;
}

// Inline function for division


inline double divide(int a, int b) {
if (b != 0) {
return static_cast<double>(a) / b;
} else {
std::cerr << "Error: Division by zero." << std::endl;
return 0;
}
}

// Inline function for modulus


inline int modulus(int a, int b) {
if (b != 0) {
return a % b;
} else {
std::cerr << "Error: Division by zero." << std::endl;
return 0;
}
}
};

int main() {
Arithmetic arith;

int a = 10, b = 5;

std::cout << "Addition of " << a << " and " << b << ": " << arith.add(a, b) << std::endl;
std::cout << "Subtraction of " << a << " and " << b << ": " << arith.subtract(a, b) << std::endl;
std::cout << "Multiplication of " << a << " and " << b << ": " << arith.multiply(a, b) << std::endl;
std::cout << "Division of " << a << " and " << b << ": " << arith.divide(a, b) << std::endl;
std::cout << "Modulus of " << a << " and " << b << ": " << arith.modulus(a, b) << std::endl;

return 0;
}

6.
#include <iostream>

// Function to swap two numbers using call by value


void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
std::cout << "Inside swapByValue function - a: " << a << ", b: " << b << std::endl;
}

int main() {
int num1 = 10;
int num2 = 20;

std::cout << "Before calling swapByValue - num1: " << num1 << ", num2: " << num2 << std::endl;

// Call the swapByValue function


swapByValue(num1, num2);

std::cout << "After calling swapByValue - num1: " << num1 << ", num2: " << num2 << std::endl;

return 0;
}

7.
#include <iostream>
#include <string>

class Employee {
protected:
std::string name;
int emp_id;
double basic_salary;

public:
// Parameterized constructor to initialize data members
Employee(const std::string& empName, int empID, double basicSal)
: name(empName), emp_id(empID), basic_salary(basicSal) {}

// Function to display employee details


void displayEmployeeDetails() const {
std::cout << "Employee Name: " << name << std::endl;
std::cout << "Employee ID: " << emp_id << std::endl;
std::cout << "Basic Salary: " << basic_salary << std::endl;
}
};

class Salary : public Employee {


private:
double gross_pay;
double net_pay;

public:
// Parameterized constructor to initialize base class members
Salary(const std::string& empName, int empID, double basicSal)
: Employee(empName, empID, basicSal), gross_pay(0), net_pay(0) {}

// Function to calculate gross and net pay


void calculatePay() {
// Example calculations for gross and net pay
// Assume some allowances and deductions for demonstration purposes
double hra = 0.2 * basic_salary; // House Rent Allowance
double da = 0.1 * basic_salary; // Dearness Allowance
double pf = 0.05 * basic_salary; // Provident Fund deduction
double tax = 0.1 * basic_salary; // Tax deduction

gross_pay = basic_salary + hra + da;


net_pay = gross_pay - (pf + tax);
}

// Function to display gross and net pay


void displayPayDetails() const {
std::cout << "Gross Pay: " << gross_pay << std::endl;
std::cout << "Net Pay: " << net_pay << std::endl;
}
};

int main() {
// Create an object of the Salary class
Salary emp("John Doe", 12345, 50000);

// Display employee details


emp.displayEmployeeDetails();

// Calculate and display pay details


emp.calculatePay();
emp.displayPayDetails();

return 0;
}

8.
#include <iostream>
#include <cmath>

// Function to calculate simple interest


double calculateSimpleInterest(double principal, double rate = 5.0, int time = 1) {
return (principal * rate * time) / 100;
}

// Function to calculate compound interest


double calculateCompoundInterest(double principal, double rate = 5.0, int time = 1, int n = 1) {
return principal * pow((1 + rate / (100 * n)), n * time) - principal;
}

int main() {
double principal;
double rate;
int time;
int n;

std::cout << "Enter the principal amount: ";


std::cin >> principal;

std::cout << "Enter the rate of interest (default is 5%): ";


std::cin >> rate;
std::cout << "Enter the time period in years (default is 1 year): ";
std::cin >> time;

std::cout << "Enter the number of times interest is compounded per year (default is 1): ";
std::cin >> n;

// Calculate simple interest with default rate and time


double simpleInterest = calculateSimpleInterest(principal, rate, time);

// Calculate compound interest with default rate, time, and compounding frequency
double compoundInterest = calculateCompoundInterest(principal, rate, time, n);

std::cout << "\nSimple Interest: " << simpleInterest << std::endl;


std::cout << "Compound Interest: " << compoundInterest << std::endl;

return 0;
}

9.
#include <iostream>

class ObjectCounter {
private:
// Static member to keep track of the number of objects created
static int objectCount;

public:
// Constructor
ObjectCounter() {
objectCount++;
std::cout << "Object created. Total objects: " << objectCount << std::endl;
}

// Destructor
~ObjectCounter() {
objectCount--;
std::cout << "Object destroyed. Total objects: " << objectCount << std::endl;
}

// Static member function to get the current count of objects


static int getObjectCount() {
return objectCount;
}
};

// Initialize static member


int ObjectCounter::objectCount = 0;

int main() {
std::cout << "Entering main block." << std::endl;
{
ObjectCounter obj1;
{
ObjectCounter obj2;
ObjectCounter obj3;
std::cout << "Current object count: " << ObjectCounter::getObjectCount() << std::endl;
}
std::cout << "Current object count after inner block: " << ObjectCounter::getObjectCount() <<
std::endl;
}
std::cout << "Current object count after main block: " << ObjectCounter::getObjectCount() <<
std::endl;
return 0;
}

10.

#include <iostream>

// Base class Shape


class Shape {
public:
// Virtual function to calculate area
virtual double calculateArea() const = 0; // Pure virtual function
};

// Class Rectangle inheriting from Shape


class Rectangle : public Shape {
protected:
double length;
double width;

public:
// Parameterized constructor
Rectangle(double l, double w) : length(l), width(w) {}

// Function to calculate area of rectangle


double calculateArea() const override {
return length * width;
}
};

// Class Cuboid inheriting from Rectangle and Shape


class Cuboid : public Rectangle {
private:
double height;

public:
// Parameterized constructor
Cuboid(double l, double w, double h) : Rectangle(l, w), height(h) {}

// Function to calculate volume of cuboid


double calculateVolume() const {
return length * width * height;
}

// Function to display area and volume


void display() const {
std::cout << "Area of rectangle base: " << calculateArea() << std::endl;
std::cout << "Volume of cuboid: " << calculateVolume() << std::endl;
}
};

int main() {
double length, width, height;

// Input dimensions
std::cout << "Enter the length of the cuboid: ";
std::cin >> length;
std::cout << "Enter the width of the cuboid: ";
std::cin >> width;
std::cout << "Enter the height of the cuboid: ";
std::cin >> height;

// Create a Cuboid object


Cuboid cuboid(length, width, height);

// Display the area and volume


cuboid.display();

return 0;
}

You might also like