Classes and inheritance

So I wrote a simple code about classes. I have two questions:

1) can I avoid giving definition of the default constructor?
2) can I put the default constructor under "private" section?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace std;

class Student{

private:
	string Name;
	string Address;
	string ID;
	string SSN;
public:
	void registerClass();
};

class fullTimeStudent : public Student{

private:
	float GPA;
public:
	void takeExam();
};

class partTimeStudent : public Student{

private:
	int numberOfCourses;
public:
	void attendClass();
};

void Student::registerClass(){

	cout << "I am in registerClass()" << endl;

}

void fullTimeStudent::takeExam(){

	cout << "I am in takeExam()" << endl;

}

void partTimeStudent::attendClass(){

	cout << "I am in attendClass()" << endl;

}

int main(){

	Student student1;
	student1.registerClass(); 

	fullTimeStudent fullTimeStudent1;
	fullTimeStudent1.takeExam();

	partTimeStudent partTimeStudent1;
	partTimeStudent1.attendClass();


	system("Pause");
	return 0;
}


void registerClass();
Last edited on
1) can I avoid giving definition of the default constructor?
Elaborate what fo you want. If you do not define any constructor, default constructor will be generated for you.
In C++11 you can explicitely state that you want compiler-generated constructor by using:
1
2
3
4
class foo
{
public:
    foo() = default;

2) can I put the default constructor under "private" section?
Yes, but there is little reason to do so.
If you do not want to have default constructor removed and uncallable, use
foo() = delete;
How can I demonstrate that it's ok not to define the default constructor with the code I provided?
In your case default constructor is generated by compiler and you are using it.
To explicitely state that you are fine with compiler generated version, you can use
1
2
3
4
class Student
{
public:
    Student() = default;


But although I didn't state that in my code, the compiler will still generate a default constructor?
Yes it will because default constructor is one of the special member functions and you are using it in your code. I fact you do stated that you want default constructor to be generated on line 53 where it was used first time.
Can you copy my code and note where does the constructor, objects reside in it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	Student student1; //Calls default constructor for Student
	student1.registerClass(); 

	fullTimeStudent fullTimeStudent1; //Calls default constructor for fullTimeStudent 
                                          //Which in order calls Student default constructor
	fullTimeStudent1.takeExam();

	partTimeStudent partTimeStudent1; //Calls default constructor for partTimeStudent 
                                          //Which in order calls Student default constructor
	partTimeStudent1.attendClass();


	system("Pause");
	return 0;
}
Last edited on
So I can't avoid not defining the default constructor in this case, because my code will just not work.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){

	
	Student.registerClass(); 

	fullTimeStudent.takeExam();

	partTimeStudent.attendClass();


	system("Pause");
	return 0;
}
So I can't avoid not defining the default constructor in this case, because my code will just not work.
Yes, you cannot. Why would you want that anyway?

Student.registerClass(); You cannot do that. Non-static member functions should be invoked on instances of class, not on the classes themself
Topic archived. No new replies allowed.