Constructors, and assigning objects to other objects

Hi, I am practicing creating classes, and everything that goes with it, as well as assigning objects to other objects. I wanted to try and assign one object of the same type to another object that was created but with a different constructor, to see if that was possible/allowed. But when i create this object in the main function, it does not show as a local variable (when i mouse over it) like the other objects I have created. I am unsure why this is, I do not know if it has something to do with the constructors? Or if for some reason I just cannot create objects the way I am creating. I have pasted my code below, if anyone could help, I would appreciate. The problem object is copyBen.

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
  #include <iostream>

class student
{
	int id;
	float credits;
	bool canGraduate = 0;

public:
	friend int getCredits(student s) { return s.credits; }
	bool getCanGraduate();

	student(int ID, float Credits);
	student();

};

bool student::getCanGraduate()
{
	if (credits >= 200)
		return true;
	else return false;
}

student::student(int ID, float Credits)
{
	id = ID;
	credits = Credits;
}

student::student() 
{
	id = 0;
	credits = 0;
}

int main()
{
	student bob(1, 150);
	student bill(2, 200);
	student ben(3, 500);
	student CopyBob(0, 0);
	student CopyBen();

	std::cout << getCredits(bob) << "\n";

	std::cout << bob.getCanGraduate() << "\n";
	std::cout << bill.getCanGraduate() << "\n";
	std::cout << ben.getCanGraduate() << "\n";

	CopyBob = bob;
	CopyBen = ben;

	std::cout << CopyBob.getCanGraduate() << "\n";
	std::cout << getCredits(CopyBob);

	std::cout << CopyBen.getCanGraduate() << "\n";
	std::cout << getCredits(CopyBen);

	return 0;

}
student CopyBen();
There is a syntax rule in C++, where, in informal terms, "If it can be a function declaration, it is a function declaration."

student CopyBen(); means "I am declaring that function exists called CopyBen, which takes in no arguments and returns an object of type student."

The zero-arg constructor (default constructor) for an object should not have () on the end of it.

You want:
student CopyBen; or equivalently, student CopyBen{};

Compare to std::string name; or int apple;
Last edited on
oh ok that makes sense. I did not know that but now that you have explained it, i understand why it would work that way. Thank you.
This feature is known as "most vexing parse in C++". See https://en.wikipedia.org/wiki/Most_vexing_parse
This builds on what you had but shows how bob, ben etc are Student objects and how to use the class methods using dot notation.

If you decide to extend the Student member list to name, ID credits etc then have a string for the Student objects name, so object 'bob' could have a name "Mary-Anne" for instance, or Student object 'anyone' could be intialized with "Bob" as a name.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>

class Student
{
private:
    int id = 999;
    float credits = 201;
    bool canGraduate = 0;
    
public:
    Student(){}; // <--
    Student(int ID, float Credits);
    
    ~Student(){}; // <--
    
    bool getCanGraduate();
    int getID(); // <--
    int getCredits(); // <--
};

Student::Student(int ID, float Credits)
{
    id = ID;
    credits = Credits;
}

int Student::getID() // <--
{
    return id;
}

int Student::getCredits() // <--
{
    return credits;
}

bool Student::getCanGraduate()
{
    if (credits >= 200)
        return true;
    else
        return false;
}

int main()
{
    Student bob(1, 150);
    Student bill(2, 200);
    Student ben(3, 500);
    Student CopyBob; // <--
    Student CopyBen; // <--
    
    std::cout << "bob's credits: " << bob.getCredits() << "\n"; // <--
    
    std::cout << "bob can graduate? " << bob.getCanGraduate() << "\n";
    std::cout << "bill can graduate? " << bill.getCanGraduate() << "\n";
    std::cout << "ben can graduate? " << ben.getCanGraduate() << "\n";
    
    CopyBob = bob;
    CopyBen = ben;
    
    std::cout << "CopyBob can graduate? " << CopyBob.getCanGraduate() << "\n";
    std::cout << "CopyBob's credits: " << CopyBob.getCredits() << '\n'; // <--
    
    std::cout << "CopyBen can graduate? " << CopyBen.getCanGraduate() << "\n";
    std::cout << "CopyBen's credits: "  << CopyBen.getCredits() << '\n'; // <--
    std::cout << "ben's credits: "  << ben.getCredits() << '\n'; // <--
    
    
    Student anyone;
    std::cout << "anyone's ID: "  << anyone.getID() << '\n'; // <--
    
    if(anyone.getCanGraduate()) // <--
        std::cout << "Can graduate\n";
    else
        std::cout << "Cannot graduate\n";
    
    std::cout << "anyone's credits: "  << anyone.getCredits() << '\n'; // <--
    
    return 0;
}
This is completely unrelated to your question, but you could shorten getCanGraduate() to:
1
2
3
4
bool Student::getCanGraduate()
{
    return (credits >= 200);
}


The expression credits>= 200 is of type bool, so you can return it directly. You could also write the statement without the parentheses (return credits >= 200;) but I find the parens clearer.
Topic archived. No new replies allowed.