C++ Assignments on Various Topics


Classes and Objects

  1. Create a class Rectangle with attributes length and width. Include methods to calculate area and perimeter of the rectangle.
  • Expected Output:
    Area: 20 square units Perimeter: 18 units
  1. Create a class Employee with attributes name, id, and salary. Implement methods to set and get employee details.
  • Expected Output:
    Employee Name: John Doe Employee ID: 101 Employee Salary: $50000
  1. Create a class Complex to perform addition and subtraction of two complex numbers.
  • Expected Output:
    Sum: 3 + 4i Difference: 1 - 2i
  1. Create a class Date with attributes day, month, and year. Implement methods to validate and display the date.
  • Expected Output:
    Date: 10/07/2024

Inheritance

  1. Create a base class Shape with methods area() and display() and derive classes Rectangle and Circle. Override the base class methods in derived classes.
  • Expected Output:
    Area of Rectangle: 20 square units Area of Circle: 78.54 square units
  1. Create a base class Person with attributes name and age. Derive classes Employee and Student from Person.
  • Expected Output:
    Employee Name: John Doe, Age: 30 Student Name: Alice Smith, Age: 20

Polymorphism

  1. Create a base class Animal with methods sound() and derive classes Dog, Cat, and Cow. Override sound() in each derived class.
  • Expected Output:
    Dog says: Woof Cat says: Meow Cow says: Moo
  1. Create a base class Shape with a pure virtual function area(). Derive classes Rectangle and Circle implementing area().
  • Expected Output:
    Area of Rectangle: 20 square units Area of Circle: 78.54 square units

Operator Overloading

  1. Create a class Matrix with overloaded operators +, -, and * to perform matrix addition, subtraction, and multiplication.
  • Expected Output: Sum of Matrices: 4 6 8 10 Difference of Matrices: 0 0 0 0 Product of Matrices: 7 10 15 22
  1. Create a class String with overloaded operator + for string concatenation.
    • Expected Output:
      Concatenated String: Hello, World!

File Handling

  1. Create a C++ program to read data from a file input.txt and write it to another file output.txt.
    • Input File (input.txt):
    Hello, this is input.txt file.
    • Output File (output.txt):
    Data copied successfully.
  2. Create a program to count the number of words, lines, and characters in a text file.
    • Input File (input.txt):
      Hello, this is a text file. This is the second line.
    • Expected Output:
      Words: 10 Lines: 2 Characters: 51

Exception Handling

  1. Create a C++ program to handle division by zero exception using try-catch block.
    • Expected Output:
    Error: Division by zero is not allowed.
  2. Create a program to handle file opening and closing exceptions using try-catch block.
    • Expected Output:
      Error: Could not open file.

Templates

  1. Create a template function maximum() to find the maximum of two numbers of any data type (int, float, double).
    • Expected Output:
    Maximum of 5 and 10: 10 Maximum of 3.5 and 2.8: 3.5
  2. Create a template class Stack to implement stack operations (push, pop, display) for any data type.
    • Expected Output:
      Stack: [5, 10, 15]

Standard Template Library (STL)

  1. Create a program to demonstrate the use of vector to store and display a list of integers.
    • Expected Output:
    Elements: 1 2 3 4 5
  2. Create a program to demonstrate the use of map to store and display student names and their marks.
    • Expected Output:
      Student Marks: John: 85 Alice: 90

String Handling

  1. Create a program to find the length of a string without using the standard library function strlen().
    • Expected Output:
    Length of String: 12
  2. Create a program to reverse a string without using the standard library function strrev().
    • Expected Output:
      Reversed String: olleH

Input and Output Manipulation

  1. Create a program to read and display integer and float values using cin and cout.
    • Input:
    Enter an integer: 5 Enter a float value: 3.14
    • Expected Output:
    Integer: 5 Float: 3.14
  2. Create a program to format output using manipulators like setw, setprecision, and fixed.
    • Expected Output:
      Formatted Output: ID Name Salary 101 John Doe $50000.50 102 Alice Smith $60000.25

Algorithms

  1. Create a program to implement bubble sort to sort an array of integers.
    • Input: Array: [5, 1, 4, 2, 8]
    • Expected Output:
    Sorted Array: 1 2 4 5 8
  2. Create a program to implement binary search to find an element in a sorted array of integers.
    • Input: Array: [1, 2, 3, 4, 5, 6, 7], Search Element: 4
    • Expected Output:
      Element found at index 3

Miscellaneous

  1. Create a program to calculate the factorial of a number using recursion.
    • Input: Number: 5
    • Expected Output:
      Factorial of 5: 120

Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment