Learning classes

I am trying to learn how to use classes, the course I am learning from is in java but I am trying to bring it over to c++ since i think it will be more useful, but it is causing me some issues. I have copied the basic setup they have given which is a school/student class and changed it to c++ but I can not get it to run as I am getting an issue which says "non-standard syntax; use '&' to create a pointer to member" for each of the getter methods. So I understand that it wants me to use a pointer, but I do not really understand why, as I have not really been introduced to pointers. Why can I not just use the code the way I am currently trying?

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
  student.header

#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED


using namespace std;

class student
{
public:

	student(string studentName, int studentID);
	~student() {};
	
	string getName();
	
	int getID();

	double getCredits();

	bool getCanGratudate();

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

};

student.cpp

	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;
	}

.main

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

using namespace std;

int main()
{
	student Dan("Dan", 234);
	cout << "Student name : " << "\t" << Dan.getName << "\n" "Student ID :" << "\t" << Dan.getID << "\n";

	system("pause");
	return 0;
	 


}
 
cout << "Student name : " << "\t" << Dan.getName() << "\n" "Student ID :" << "\t" << Dan.getID() << "\n";
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
// THIS GOES IN .h FILE
#include <string>
#include <iostream>

class Student
{
private:
    std::string name;
    int id;
    double credits;
    bool graduate;
    
public:
    Student(std::string studentName, int studentID)
    {
        name = studentName;
        id = studentID;
    }
    
    ~Student() {};
    
    std::string getName();
    
    int getID();
    
    double getCredits();
    
    bool getCanGratudate();
};
///////////////

// THIS GOES IN .cpp FILE
std::string Student::getName()
{
    return name;
}

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

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

bool Student::getCanGratudate()
{
    return graduate;
}
///////////////


int main()
{
    Student Dan("Dan", 234);
    std::cout
    << "Student name : " << '\t' << Dan.getName() << '\n'
    << "  Student ID : " << '\t' << Dan.getID() << '\n';
    
    return 0;
}



Student name : 	Dan
  Student ID : 	234
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.