Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
heeey everyone!!!
what i'm doning wrong here,
tring to pass a value to an insert in class method ?

Implement a class course to manage the participants of a
Course.
Realize a constructor with which the title of the course can be determined;

and methods for entering names and student numbers for the individual female/male 
students,
which are identified by a number assigned to you (i.e., an index).
It must be possible (through appropriate methods)

thank!!!

What I have tried:

<pre>#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_mrt_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_mrt_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_mrt_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();
		mycourse().add(st);
		


	}
C++

Posted
Updated 30-Nov-18 2:30am
v3
Comments
Xynosural.net 30-Nov-18 7:32am    
More info ? what do you want to do what goes wrong ?
Anas Zahed 30-Nov-18 7:38am    
i'm getting lots of error !!!
tried to lose them but diding word
Patrice T 30-Nov-18 7:49am    
and the errors messages are ?
Anas Zahed 30-Nov-18 8:03am    
if i don it with Vector i wont get any error but i want to do it with map

1 solution

Fixed for you:
Please note: C++, unilike C# has NO syntactic sugar for object properties, you have to use standard syntax for method invocation.

C++
#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:
C++
#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();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900