Fixed for you:
Please note:
C++
, unilike
C#
has NO syntactic sugar for object properties, you have to use standard syntax for method invocation.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class Student
{
private:
std::string name;
int mrt_nr;
public:
void set_name(string);
void set_nr(int);
string get_name();
int get_nr();
};
void Student::set_name(string a)
{
name = a;
}
void Student::set_nr(int a)
{
mrt_nr = a;
}
string Student::get_name() {
return name;
}
int Student::get_nr() {
return mrt_nr;
}
class Course
{
private:
string titel_lehrv;
unordered_map<int, std::string> st;
public:
Course(const std::string &titel) : titel_lehrv{ titel } {};
void add( Student &a ) {
st.insert(pair<int, string>(a.get_nr(), a.get_name()));
}
};
int main()
{
pair<int, std::string>s1(12545, "tom");
Student st;
st.set_name("tom");
st.set_nr(123);
Course mycourse("C++ programming");
mycourse.add(st);
}
Your code could be more terse:
#include <iostream>
#include <unordered_map>
using namespace std;
class Student
{
private:
string name;
int no;
public:
Student(string name, int no):name(name), no(no){}
void set_name(const string & new_name){name = new_name;}
string get_name() const { return name;}
void set_no(int new_no){no = new_no;}
int get_no() const { return no;}
};
class Course
{
private:
string title;
unordered_map < int, string > student_map;
public:
Course( const string title ) : title(title){}
void add( const Student & student)
{
student_map[student.get_no()] = student.get_name();
}
void print() const
{
cout << title << "\n";
for (const auto & [ no, name ] : student_map)
cout << no << ", " << name << "\n";
}
};
int main()
{
Course course("C++ programming");
course.add( Student("john", 10));
course.add( Student("jim", 100001));
course.add( Student("tom", 20210));
course.print();
}