CSE224 / C++ Lab
Syllabus
Resources
EXPT 1 - Classes & Objects
Write a program that uses a class where the member functions are defined inside a class.
#include <iostream>
using namespace std;
// Define the class
class Calculator {
public:
// Member function to add two numbers
int add(int a, int b) {
return a + b;
}
// Member function to subtract two numbers
int subtract(int a, int b) {
return a - b;
}
// Member function to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
// Member function to divide two numbers
double divide(int a, int b) {
if (b != 0) {
return static_cast<double>(a) / b;
} else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
};
int main() {
Calculator calc;
// Get input from the user
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
// Perform operations
cout << "Addition: " << calc.add(num1, num2) << endl;
cout << "Subtraction: " << calc.subtract(num1, num2) << endl;
cout << "Multiplication: " << calc.multiply(num1, num2) << endl;
cout << "Division: " << calc.divide(num1, num2) << endl;
return 0;
}EXPT 2 - Classes & Objects
Write a program that uses a class where the member functions are defined outside a class.
#include <iostream>
using namespace std;
// Define the class
class Calculator {
public:
// Member functions declared inside the class
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
double divide(int a, int b);
};
// Define the member functions outside the class
int Calculator::add(int a, int b) {
return a + b;
}
int Calculator::subtract(int a, int b) {
return a - b;
}
int Calculator::multiply(int a, int b) {
return a * b;
}
double Calculator::divide(int a, int b) {
if (b != 0) {
return static_cast<double>(a) / b;
} else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
int main() {
Calculator calc;
// Get input from the user
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
// Perform operations
cout << "Addition: " << calc.add(num1, num2) << endl;
cout << "Subtraction: " << calc.subtract(num1, num2) << endl;
cout << "Multiplication: " << calc.multiply(num1, num2) << endl;
cout << "Division: " << calc.divide(num1, num2) << endl;
return 0;
}EXPT 3 - Classes & Objects
Write a Program to Demonstrate Inline functions.
#include <iostream>
using namespace std;
// Define a class to demonstrate inline functions
class MathOperations {
public:
// Inline function to calculate the square of a number
inline int square(int num) {
return num * num;
}
// Inline function to calculate the cube of a number
inline int cube(int num) {
return num * num * num;
}
};
int main() {
MathOperations math;
// Get input from the user
int number;
cout << "Enter a number: ";
cin >> number;
// Demonstrate the use of inline functions
cout << "Square of " << number << " is: " << math.square(number) << endl;
cout << "Cube of " << number << " is: " << math.cube(number) << endl;
return 0;
}EXPT 5 - Constructor & Destructor
Write a program to demonstrate the use of zero argument and parameterized constructors.
EXPT 6 - Operator Overloading
Write a program to demonstrate the overloading of increment and decrement operators.
EXPT 9 - Inheritance
Write a Program to demonstrate use of protected members, public & private protected classes, multilevel inheritance etc.
EXPT 12 - Templates & Generic Programming
Write a program to demonstrate the use of function template.
Assignment Questions
[⤓] [PDF] Assignment-CSE224-Btech
Last updated
