Constructor doesn't work

Hello! every one.
The following program works
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
#include<iostream>
#include<conio.h>
using namespace std; 
class student
{
public:
    int id;
    double gpa ;
    void display()
    {
        cout << id <<"  "<<gpa<<'\n';
    }
   void setvalue(int x, double y)
    {
        id = x;
        gpa = y;
    }
};
int main()
{
    student promod;
    promod.id = 062010;
    promod.gpa = 3.05;
    cout <<promod.id<<"   "<< promod.gpa << '\n';
    student nisha;
    nisha.id = 875670;
    nisha.gpa = 3.27;
    cout <<nisha.id<<"   " << nisha.gpa << '\n';
    student ahenjita;
    ahenjita.id = 012021;
    ahenjita.gpa = 123456;
    ahenjita.display();
    student pronita;
    pronita.setvalue(253, 466);
    pronita.display();
    _getch();
}


The above program works! But when I add constructor then the program doesn't work. Means the following program doesn't works. Could someone please explain?


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
#include<iostream>
#include<conio.h>
using namespace std; 
class student
{
public:
    int id;
    double gpa ;
    void display()
    {
        cout << id <<"  "<<gpa<<'\n';
    }
   void setvalue(int x, double y)
    {
        id = x;
        gpa = y;
    }
    student(int x, double y)
    {
        id = x;
        gpa = y;
    }
};
int main()
{
    student promod;
    promod.id = 062010;
    promod.gpa = 3.05;
    cout <<promod.id<<"   "<< promod.gpa << '\n';
    student nisha;
    nisha.id = 875670;
    nisha.gpa = 3.27;
    cout <<nisha.id<<"   " << nisha.gpa << '\n';
    student ahenjita;
    ahenjita.id = 012021;
    ahenjita.gpa = 123456;
    ahenjita.display();
    student pronita;
    pronita.setvalue(253, 466);
    pronita.display();
    student pronob(52, 31);
    pronob.display();
    _getch();
}
Last edited on
If a class doesn't have any explicitly-defined constructors, the compiler automatically defines a default constructor that takes no parameters. If the class does explicitly define a constructor, the compiler omits the default constructor If you want the class to still have its compiler-defined default constructor, do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class student
{
public:
    int id;
    double gpa ;
    void display()
    {
        cout << id <<"  "<<gpa<<'\n';
    }
   void setvalue(int x, double y)
    {
        id = x;
        gpa = y;
    }
    student() = default; // <----
    student(int x, double y)
    {
        id = x;
        gpa = y;
    }
};

By the way, by convention constructors are always put at the top of a class definition, just below the data members.
and default initialise id and gpa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class student
{
public:
	int id {};
	double gpa {};

	void display() const
	{
		cout << id << "  " << gpa << '\n';
	}

	void setvalue(int x, double y)
	{
		id = x;
		gpa = y;
	}

	student() = default;
	student(int x, double y) : id(x), gpa(y) {}
};



Topic archived. No new replies allowed.