Assignment

Write your question here.

Create a class called student. This class contains data members for name, roll no, and CGPA of a student.
Provide a no-argument constructor for initializing the data members to some fixed value.
Provide a 2-argument constructor to initialize the data members to the values sent from the calling function.
Provide separate setter functions for setting each data member. These functions should take the values from user at run-time.
Provide separate getter functions for each data member. The getter functions should return the value of the corresponding fields.
Create a function display that displays all the information to user.
Let us suppose that we want to keep information about average CGPA of students in a particular department. Make appropriate changes in the class to handle this extra information (Hint: provide static data members for average CGPA and no of students and set the values for these members in constructor). Provide a static function to display this additional information.
Hello Abdulahad,

To start with rework your instructions this way:

1.) Create a class called student. This class contains data members for name, roll no, and CGPA of a student.

2.) Provide a no-argument constructor for initializing the data members to some fixed value.

3.) Provide a 2-argument constructor to initialize the data members to the values sent from the calling function.

4.) Provide separate setter functions for setting each data member. These functions should take the values from user at run-time.

5.) Provide separate getter functions for each data member. The getter functions should return the value of the corresponding fields.

6.) Create a function display that displays all the information to user.

7.) Let us suppose that we want to keep information about average CGPA of students in a particular department.

7a.) Make appropriate changes in the class to handle this extra information (Hint: provide static data members for average CGPA and no of students and set the values for these members in constructor). Provide a static function to display this additional information.


Now start with #1. It might loolksomething like this:
Student.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef STUDENT
#define STUDENT

class Student
{
	public:
		Student();
		~Student();

		Student(int pram1, int pram2);

		// <--- Stter functions.

		// <--- Getter functions.

		// <--- Other functions.
	private:
		// <--- Your variables here.
};

#endif // !STUDENT 


Student.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include <string>

#include "Student.hpp"

Student::Student()  //<--- Default ctor
{
}

Student::~Student()  //<--- Default dtor
{
}

Student::Student(int pram1, int pram2)  //<--- Overloaded ctor
{

}

//  <--- Setter functions.

// <--- Getter functions.

// <--- Other functions for the class. 


Main.cpp. From Furry Guy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>
#include <limits>

#include "Student.hpp"

int main()
{
	std::cout << "Do you want someone to do all the work for you? ";
	char answer{};
	std::cin >> answer;

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	if (std::toupper(answer) == 'Y') { std::cout << "Post it in the Jobs section.\n"; }

	else { std::cout << "Show what you have coded so far.\n"; }

	std::cout << "Good luck.\n";
}


Hope that helps,

Andy

P.S. Posting the same subject twice does not improve your responces. It would be best to delete http://www.cplusplus.com/forum/beginner/252567/
Handy Andy wrote:
1
2
3
4
5
class Student
{
    public:
        Student();
        ~Student();


1
2
3
4
5
6
7
Student::Student()  //<--- Default ctor
{
}

Student::~Student()  //<--- Default dtor
{
}

Don’t you like the “Rule of zero”?
https://en.cppreference.com/w/cpp/language/rule_of_three (end of page)

In my opinion, is one of the best ‘rules’ in C++: just don’t do anything, and the compiler will write the proper functions for you. Let me say I love it :-)
@Enoizat,

Don’t you like the “Rule of zero”?
Yes I do, but I am not as use to it as I should be.

In my opinion, is one of the best ‘rules’ in C++: just don’t do anything, and the compiler will write the proper functions for you.
I have been working with the understanding that if a default ctor and dtor are not provided in the class then the compiler will provide on. But if an overloaded ctor is available in the class then the writer must provide the default ctor and dtor as the compiler will not provide one because of the overloaded ctor.

I have seen this in other programs where the writer will use an overloaded ctor in the class and then in "main" the first thing they do is write Foo bar; invoking the default ctor that does not exist. If this did not elicit a compiler error then it did cause the program to crash at run time. It has been awhile, but I believe it produces a compiler error not being able to find the default ctor.

It it entirely possible that I misunderstood something here, but I have yet to see anything to correct me.

Lastly after typing the word "class" VS filled in the code that you see with the addition of the overloaded ctor example I put in and splitting into to files. With the comments in the ".cpp" I added.

If I have something wrong here let me know.

Andy
But if an overloaded ctor is available in the class then the writer must provide the default ctor and dtor as the compiler will not provide one because of the overloaded ctor.

No, the default destructor can still be used with any number of constructors.


I have seen this in other programs where the writer will use an overloaded ctor in the class and then in "main" the first thing they do is write Foo bar; invoking the default ctor that does not exist.


Instead of creating an empty "default" constructor you can use the "default" keyword, if using modern C++, instead of writing your own empty function. Also the destructor is only really needed when you have pointer member variables, otherwise the default destructor is usually sufficient. And when you do require the destructor you should also follow the Rule of Three/Five and create the copy, asignment, move operations.


Lastly after typing the word "class" VS filled in the code that you see with the addition of the overloaded ctor example I put in and splitting into to files. With the comments in the ".cpp" I added.

This sounds like VS is using a "default" class template to generate the code you see. There are probably ways of altering this "default", check your IDE documentation.

1
2
3
4
5
6
7
8
9
10
...
class Student
{
	public:
		Student() = default;  // Let the compiler create the default constructor.
		//~Student(); // Not needed.

		Student(int pram1, int pram2);
...
 


@jlb,

Thanks for the input. I did not understand correctly that the compiler would still create a default dtor.

And yes I know that I can find and change the file that stores the the information about the class template. I have not had any reason to change it yet. I will have to work on that.

Andy
Topic archived. No new replies allowed.