Skip to content

Commit 1a34cc3

Browse files
Added documentation for functions
Added documentation for functions
1 parent eb06cd1 commit 1a34cc3

File tree

5 files changed

+294
-9
lines changed

5 files changed

+294
-9
lines changed

Constructor/Copy-constructor-code.cpp

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1+
/**
2+
* @brief This program demonstrates the usage of constructors in a Student class.
3+
*
4+
* The Student class has member variables for roll number, name, and id.
5+
* It includes a parameterized constructor and a copy constructor.
6+
* The parameterized constructor initializes the roll number, name, and id.
7+
* The copy constructor creates a new student object by copying the values from another student object.
8+
*
9+
* The main function prompts the user to enter roll number, name, and id number.
10+
* It then creates two student objects: s1 using the parameterized constructor,
11+
* and s2 using the copy constructor.
12+
*/
13+
14+
// Including Header File
115
#include <iostream>
2-
#include <stdio.h>
16+
17+
// Using Namespace
318
using namespace std;
19+
20+
/// @brief Class for Student object.
421
class student
522
{
6-
//wap for copy constructor
723
public:
824
int roll;
925
string name;
26+
1027
private:
1128
int id;
29+
1230
public:
31+
/**
32+
* @brief Constructor for the student class that initializes the roll number, name, and id.
33+
*
34+
* This constructor takes three parameters: roll number (n1), name (n2), and id (n3).
35+
* It assigns the values of n1, n2, and n3 to the corresponding member variables roll, name, and id.
36+
* The id is assigned twice, which is redundant and can be optimized.
37+
*
38+
* @param n1 The roll number of the student.
39+
* @param n2 The name of the student.
40+
* @param n3 The id of the student.
41+
*
42+
* @return No return value.
43+
*/
44+
1345
student(int n1, string n2, int n3)
1446
{
1547
roll = n1;
@@ -21,6 +53,23 @@ class student
2153
cout << "Name is:" << name << endl;
2254
cout << "Id is:" << id << endl;
2355
}
56+
57+
/**
58+
* @brief Copy constructor for the student class.
59+
*
60+
* This copy constructor creates a new student object by copying the values of another student object.
61+
* It initializes the roll number, name, and id of the new object with the corresponding values from the given student object.
62+
*
63+
* @param s1 The reference to the student object from which the values will be copied.
64+
*
65+
* @return No return value.
66+
*
67+
* @note The copy constructor is called when a new object is created as a copy of an existing object.
68+
* @note The copy constructor is automatically called when an object is passed by value or returned by value.
69+
* @note The copy constructor is not called when an object is passed by reference or returned by reference.
70+
* @note The copy constructor can be explicitly called using the syntax: student s2(s1); or student s2 = s1;
71+
*/
72+
2473
student(const student &s1)
2574
{
2675
roll = s1.roll;
@@ -32,6 +81,19 @@ class student
3281
cout << "Id is:" << id << endl;
3382
}
3483
};
84+
85+
/**
86+
* @brief The main function of the program.
87+
*
88+
* This function prompts the user to enter roll number, name, and id number.
89+
* It then creates two student objects: s1 using the parameterized constructor,
90+
* and s2 using the copy constructor.
91+
*
92+
* @return 0 to indicate successful program execution.
93+
*
94+
* @note The main function demonstrates the usage of the student class constructors.
95+
*/
96+
3597
int main()
3698
{
3799
int x, z;
Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,72 @@
1+
/**
2+
* @brief This program demonstrates the usage of constructors and destructors in a C++ class.
3+
*
4+
* The calc class is defined with a constructor and a destructor.
5+
* The constructor increments a static counter each time an object is created,
6+
* and the destructor decrements the counter each time an object is destroyed.
7+
* The main function creates four instances of the calc class,
8+
* which triggers the constructor and destructor for each object.
9+
*
10+
* The program outputs the number of objects created and destroyed at each step.
11+
* This helps illustrate the concept of object lifetime management in C++.
12+
*
13+
* @return 0 - Indicates successful program execution.
14+
*/
15+
16+
/*WAP in C++ which defines a class with constructor and destructor
17+
which will count number of objects created and destroyed.*/
18+
19+
// Including Header File
120
#include <iostream>
21+
22+
// Using Namespace
223
using namespace std;
24+
25+
// Declaring Static Variable
326
int count = 0;
4-
/*WAP in C++ which defines a class with constructor and destructor
5-
which will count number of objects created and destroyed.*/
27+
28+
/// @brief Class for Calculation
629
class calc
730
{
831
public:
32+
/**
33+
* @brief Constructor for the calc class.
34+
*
35+
* This constructor increments the static count variable each time an object of the calc class is created.
36+
* It then outputs the current number of objects created to the console.
37+
*/
38+
939
calc()
1040
{
1141
count++;
1242
cout << "Number of objects created:" << count << endl;
1343
}
44+
45+
/**
46+
* @brief Destructor for the calc class.
47+
*
48+
* This destructor decrements the static count variable each time an object of the calc class is destroyed.
49+
* It then outputs the current number of objects remaining to the console.
50+
*/
51+
1452
~calc()
1553
{
1654
cout << "Number of objects destroyed:" << count << endl;
1755
count--;
1856
}
1957
};
58+
59+
/**
60+
* @brief The main function of the program.
61+
*
62+
* This function creates four instances of the calc class and then exits.
63+
* The calc class has a constructor and destructor that keep track of the number of objects created and destroyed.
64+
*
65+
* @return 0 - Indicates successful program execution.
66+
*/
67+
2068
int main()
2169
{
22-
calc c1, c2, c3, c4;
70+
calc c1, c2, c3, c4; // Creating four instances of the calc class
2371
return 0;
2472
}

Constructor/Default-constructor-code.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1+
/**
2+
* @brief This program demonstrates the usage of a simple class representing flowers.
3+
*
4+
* The program defines a class called 'flower' with private member variables for two flower names.
5+
* It includes a default constructor that initializes the flower names and prints out a message.
6+
* The main function creates an instance of the flower class and terminates the program.
7+
*/
8+
9+
// Including Header File
110
#include <iostream>
11+
12+
// Using Namespace
213
using namespace std;
14+
15+
// Defining the flower class
316
class flower
417
{
518
private:
619
string f1;
720
string f2;
21+
822
public:
23+
/**
24+
* @brief Default constructor for the flower class.
25+
*
26+
* This constructor initializes the private member variables f1 and f2 with default values.
27+
* It also prints out a message indicating that the default constructor has been called,
28+
* along with the names of the two flowers.
29+
*
30+
* @param None
31+
* @return None
32+
*/
33+
934
flower()
1035
{
1136
f1 = "Lily";
@@ -15,6 +40,17 @@ class flower
1540
cout << "Second flower is:" << f2 << endl;
1641
}
1742
};
43+
44+
/**
45+
* @brief The main function of the program.
46+
*
47+
* This function serves as the entry point for the program. It creates an instance of the flower class
48+
* and then returns 0 to indicate successful program termination.
49+
*
50+
* @param None
51+
* @return 0 - Indicates successful program termination.
52+
*/
53+
1854
int main()
1955
{
2056
flower fl;

Constructor/Destructor-code.cpp

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,127 @@
1+
/**
2+
* @brief This program demonstrates the usage of a simple point class in C++.
3+
*
4+
* The point class has attributes for length and provides methods to set and retrieve the length value.
5+
* The main function creates two point objects, sets their lengths, and prints their lengths.
6+
*/
7+
8+
// Including Header File
19
#include <iostream>
10+
11+
// Using Namespace
212
using namespace std;
13+
14+
// Definition of Point Class
315
class point
416
{
517
public:
18+
/**
19+
* @brief Sets the length of the point object.
20+
*
21+
* This function updates the Length attribute of the point object with the provided length value.
22+
*
23+
* @param len The new length value to be assigned to the point object.
24+
*
25+
* @return void
26+
*/
627
void setLength(int len);
28+
29+
/**
30+
* @brief Retrieves the length of the point object.
31+
*
32+
* This function retrieves the current length value of the point object.
33+
*
34+
* @return An integer representing the length of the point object.
35+
*/
36+
737
int getLength(void);
38+
39+
/**
40+
* @brief Default constructor for the point class.
41+
*
42+
* This constructor initializes a new point object with default values.
43+
* The Length attribute is set to 0.
44+
*
45+
* @return void
46+
*/
847
point();
48+
49+
/**
50+
* @brief Destructor for the point class.
51+
*
52+
* This destructor is responsible for cleaning up the resources allocated by the point object.
53+
* It prints a message indicating that the object is being deleted and displays its current length.
54+
*
55+
* @return void
56+
*/
957
~point();
58+
1059
private:
1160
double Length;
1261
};
62+
63+
/**
64+
* @brief Default constructor for the point class.
65+
*
66+
* This constructor initializes a new point object with default values.
67+
* The Length attribute is set to 0.
68+
*
69+
* @return void
70+
*/
1371
point::point(void)
1472
{
1573
cout << "object is being created" << endl;
1674
}
75+
76+
/**
77+
* @brief Destructor for the point class.
78+
*
79+
* This destructor is responsible for cleaning up the resources allocated by the point object.
80+
* It prints a message indicating that the object is being deleted and displays its current length.
81+
*
82+
* @return void
83+
*/
1784
point::~point(void)
1885
{
1986
cout << "object is being deleted:" << getLength() << endl;
2087
}
88+
89+
/**
90+
* @brief Sets the length of the point object.
91+
*
92+
* This function updates the Length attribute of the point object with the provided length value.
93+
*
94+
* @param len The new length value to be assigned to the point object. It should be a non-negative integer.
95+
*
96+
* @return void
97+
*/
2198
void point::setLength(int len)
2299
{
23100
Length = len;
24101
}
102+
103+
/**
104+
* @brief Retrieves the length of the point object.
105+
*
106+
* This function retrieves the current length value of the point object.
107+
*
108+
* @return An integer representing the length of the point object.
109+
* The length value is a non-negative integer.
110+
*/
25111
int point::getLength(void)
26112
{
27113
return Length;
28114
}
29115

116+
/**
117+
* @brief The main function of the program.
118+
*
119+
* This function demonstrates the usage of the point class by creating two point objects,
120+
* setting their lengths, and printing their lengths.
121+
*
122+
* @return An integer representing the exit status of the program.
123+
* A return value of 0 indicates successful execution.
124+
*/
30125
int main()
31126
{
32127
point l;
@@ -36,4 +131,4 @@ int main()
36131
l1.setLength(60);
37132
cout << "Length of object is:" << l1.getLength() << endl;
38133
return 0;
39-
}
134+
}

0 commit comments

Comments
 (0)