Professional Documents
Culture Documents
c++ 1-10
c++ 1-10
#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) {}
int main() {
// Create an object of the Person class and initialize it
Person person("John Doe", 30);
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') {}
int main() {
const int numberOfStudents = 3;
Student students[numberOfStudents];
return 0;
}
3.
#include <iostream>
// Global variable
int value = 10;
class Example {
public:
// Class member variable
static int value;
int main() {
// Local variable in main
int value = 30;
Example ex;
ex.displayValues(value);
return 0;
}
4.
#include <iostream>
#include <cmath>
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;
}
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>
int main() {
int num1 = 10;
int num2 = 20;
std::cout << "Before calling swapByValue - num1: " << num1 << ", num2: " << num2 << std::endl;
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) {}
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) {}
int main() {
// Create an object of the Salary class
Salary emp("John Doe", 12345, 50000);
return 0;
}
8.
#include <iostream>
#include <cmath>
int main() {
double principal;
double rate;
int time;
int n;
std::cout << "Enter the number of times interest is compounded per year (default is 1): ";
std::cin >> n;
// Calculate compound interest with default rate, time, and compounding frequency
double compoundInterest = calculateCompoundInterest(principal, rate, time, n);
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;
}
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>
public:
// Parameterized constructor
Rectangle(double l, double w) : length(l), width(w) {}
public:
// Parameterized constructor
Cuboid(double l, double w, double h) : Rectangle(l, w), height(h) {}
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;
return 0;
}