0% found this document useful (0 votes)
6 views7 pages

C++ Programs for Classes and Functions

The document outlines seven different programs that implement various classes and member functions in C++. Each program covers a specific concept such as complex numbers, 2-D points, harmonic progression, solid objects, time functions, matrix operations, and string manipulation. Each section includes steps for defining classes, constructors, member functions, and the main function to execute the program.

Uploaded by

meenamuthu146
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

C++ Programs for Classes and Functions

The document outlines seven different programs that implement various classes and member functions in C++. Each program covers a specific concept such as complex numbers, 2-D points, harmonic progression, solid objects, time functions, matrix operations, and string manipulation. Each section includes steps for defining classes, constructors, member functions, and the main function to execute the program.

Uploaded by

meenamuthu146
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Complex Number Using Member Functions

STEP 1. Start the program

STEP 2: Define the class Complex with private data members real and imag to store the real and imaginary parts.

STEP 3: Create a constructor Complex(double r, double i) to initialize the complex number.

STEP 4: Implement the setComplex(double r, double i) function to set the value of the complex number.

STEP 5: Implement the showComplex() function to display the complex number.

STEP 6: Implement the add(Complex c) function to add two complex numbers.

STEP 7: Implement the subtract(Complex c) function to subtract two complex numbers.

STEP 8: Implement the multiply(Complex c) function to multiply two complex numbers.

STEP 9: Implement the multiplyScalar(double scalar) function to multiply the complex number with a scalar value.

STEP 10: Enter the main function and call the corresponding methods print the value

STEP 10: Stop the program.


2. Program for 2-D Point Using Member Functions

STEP 1. Start the program

STEP 2. Define a class named Point with private member variables x and y to represent the coordinates.

STEP 3. Create a constructor Point(double x = 0, double y = 0) to initialize the point.

STEP 4. Write a member function void set(double x, double y) to set the value of a point.

STEP 5. Write a member function void show() to display the value of a point.

STEP 6. Write a member function double distance(Point other) to calculate the distance between two points using the
distance formula.

STEP 7. Write a member function bool isEqual(Point other) to check whether two points are equal by comparing their
coordinates.

STEP 8: Enter the main function and call the corresponding methods print the value

STEP 9: Stop the program.


3. Program for Harmonic Progression (HP) To Implement Functions

STEP 1. Start the program

STEP 2. Define a class HarmonicProgression with a private member variable a (first term) and d (common difference).

STEP 3. Create a constructor HarmonicProgression(double a = 1, double d = 1) to initialize the HP.

STEP [Link] a friend class ArithmeticProgression with similar members and functions.

STEP 5. Write a member function void generateHP(int n) to generate the HP up to n terms.

STEP 6. Write a member function double sumToN(int n) to calculate the sum of the HP to n terms.

STEP 7. Write a member function double sumToInfinity() to calculate the sum of the HP to infinity.

STEP 8. Write a member function double nthTerm(int n) to generate the `n`th term of the HP.

STEP 9. Write a friend function ArithmeticProgression getCorrespondingAP() to generate the corresponding Arithmetic
Progression.

STEP 10. Enter the main function and call the corresponding methods print the value

STEP 11. Stop the program.


4. Program Use Functions To Calculate Volume And Surface Area For Different Solid Object

STEP 1. Start the program

STEP 2. Define a class Solid with private member variables:

- double length, width, height to represent dimensions

- std::string type to specify the type of solid (e.g., "cube", "rectangular prism", "sphere", etc.)

STEP 3. Create a constructor Solid(double length, double width, double height, std::string type) to initialize the solid.

STEP 4. Write a member function double calculateVolume() to calculate the volume of the solid based on its type.

STEP 5. Write a member function double calculateSurfaceArea() to calculate the surface area of the solid based on its
type.

STEP 6. Use if-else statements or polymorphism to handle different types of solids.

STEP 7. Enter the main function and call the corresponding methods print the value

STEP 8. Stop the program.


5. Program for Time Functions Using Functions

STEP 1. Start the program

STEP 2. Define a class Time with private member variables:

- int hours, minutes, seconds to represent time in hh:mm:ss format

STEP 3. Create a constructor Time(int hours = 0, int minutes = 0, int seconds = 0) to initialize the time.

STEP 4. Write a member function void setTime(int hours, int minutes, int seconds) to set the time.

STEP 5. Write a member function void showTime() to display the time in hh:mm:ss format.

STEP 6. Write a member function Time difference(Time other) to find the difference between two time objects.

STEP 7. Write a member function void addDuration(int hours, int minutes, int seconds) to add a given duration to the
time.

STEP 8. Write a member function int toSeconds() to convert the time object to seconds.

STEP 9. Enter the main function and call the corresponding methods print the value

STEP 10. Stop the program


6. Program for Addition And Multiplication Of Two Matrices Using Operator Overloading

STEP 1. Start the program

STEP 2. Define a class Matrix with private member variables:

- double data[3][3] to represent the 3x3 matrix

- static int count to maintain the count of matrix objects created

STEP 3. Create a constructor Matrix() to initialize the matrix with zeros and increment the count.

STEP 4. Write a member function void display() to display the matrix.

STEP 5. Overload the + operator to add two matrices.

STEP 6. Overload the * operator to multiply two matrices.

STEP 7. Write a static member function int getCount() to return the count of matrix objects created.

STEP 8. Enter the main function and call the corresponding methods print the value

STEP 9. Stop the program


7. Program for String Function Using An Array Of Size 100.

STEP 1. Start the program

1. Define a class cString with a private member variable:

- char str[100] to represent the string

2. Create a constructor cString(const char* initStr = "") to initialize the string.

3. Write a copy constructor cString(const cString& other) to copy a string.

4. Write a member function void concatenate(const cString& other) to concatenate two strings.

5. Write a member function int length() to find the length of the string.

6. Write a member function void reverse() to reverse the string.

7. Write a member function bool compare(const cString& other) to compare two strings.

STEP 8. Enter the main function and call the corresponding methods print the value

STEP 9. Stop the program

You might also like