Private variables and errors with methods

Hi, I am trying to get some practice with creating classes in headers and cpp files, and having some issues. My first issue is setting the private name and id variables to the parameters, I thought since they were in the headers they would be found but they do not seem to be, so i am wondering how that should be done. My other issue is with my methods, I do not understand the issue with theses, the error i am getting is "qualified name is not allowed in member declaration" and when i search this, the only thing I seem to find, is something to do with directx9, so I sm at a loss there, so hoping someone can point out where i have went wrong with them.
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
  header

#pragma once


using namespace std;

class student
{
public:
	//getter for name

	string getName();
	
	//getter for id

	int getID();
	//getter for credits

	double getCredits();

	//getter for can gratuate

	bool getCanGratudate();

private:
	string name;
	int id;
	double credits;
	bool graduate;

};




cpp

#include <iostream>
#include <string>
#include "student.h"

class student
{
	student(string studentName, int studentID)
	{
		name = studentName;
		id = studentID
	}

	string student::getName()
	{
		return name;
	}

	int student::getID()
	{
		return id;
	}
	double student::getCredits()
	{
		return credits;
	}
	bool student::getCanGratudate()
	{
		return graduate;
	}
};
Header:
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
// #include guard: https://en.wikipedia.org/wiki/Include_guard
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED

// using namespace std; // avoid this in a header file

#include <string>

class student
{
    public:

        student() = default ; // http://www.stroustrup.com/C++11FAQ.html#default
        student( std::string name, int id ) ;


        // const: https://isocpp.org/wiki/faq/const-correctness#const-member-fns
        std::string name() const ;
        int id() const ;
        double credits() const ;
        bool can_graduate() const ;

        // ...

    private:

        // http://www.stroustrup.com/C++11FAQ.html#member-init
        std::string name_;
        int id_ = 0 ;
        double credits_ = 0 ;
        bool can_graduate_ = false ;

};

#endif // STUDENT_H_INCLUDED 


Implementation (.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "student.h" // include the header

// https://en.cppreference.com/w/cpp/language/initializer_list
student::student( std::string name, int id ) : name_(name), id_(id)
{
    // validate name, id; correct or throw on validation failure
    // comput grade, can_graduate etc.
}

std::string student::name() const { return name_ ; }

int student::id() const { return id_ ; }

double student::credits() const { return credits_ ; }

bool student::can_graduate() const { return can_graduate_ ; }

// etc. 

Most of what you have is OK. the main problem is lines 43 44 and 68.
I've tidied up your code a little and it runs:
1. Comments that repeat obvious code are not good practice.
2. New types/classes are named with a starting capital letter. i.e. Student not student
3. 'Gratudate' is not what you meant.

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
// HEADER (.h) FILE

#include <string>

using namespace std;

class Student
{
private:
    string name;
    int id;
    double credits;
    bool graduate;
    
public:
    Student(string = "Who knows??", int = -1);
    ~Student(){};
    
    string getName();
    int getID();
    double getCredits();
    bool getCanGraduate();
};


// IMPLEMENTATION (.cpp) FILE
#include <iostream>
// #include "student.h"

Student::Student(string studentName, int studentID)
{
    name = studentName;
    id = studentID;
}

string Student::getName()
{
    return name;
}

int Student::getID()
{
    return id;
}

double Student::getCredits()
{
    return credits;
}

bool Student::getCanGraduate()
{
    return graduate;
}

// TEST
int main()
{
    Student bob("Bob", 123);
    cout << bob.getName() << ' ' << bob.getID() << '\n';
    
    Student Bill;
    cout << Bill.getName() << ' ' << Bill.getID() << '\n';
    
    return 0;
}


Edit: threw in free of charge defaults to constructor
Last edited on
Topic archived. No new replies allowed.