Classes

In class Persoana, how to use the constructors? In main i will get errors with p1 and p2 objects because of that. I am not allowed the change the main() or to add more private data.

#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;

class Profesie
{
public:
Profesie(string r, int s) //constructor
{
setRol(r);
setSalariu(s);
}


string setRol(string job){ rol=job;} //functii getter si setter pentru datele membre
string getRol(){return rol;}
void setSalariu(int s){ salariu = s;}
int getSalariu(){return salariu;}
// string setJob(string job, int s){rol=job, s=salariu;}
ostream& operator<<(ostream&);

private://datele membre
string rol;
int salariu;

};

class Persoana
{
public:
//constructor
Persoana(string n, string p, string g, int n) //: Job(j)
{

setnume(n);
setprenume(p);




}
Persoana(string n, string p, Profesie& j)
{
setnume(n);
setprenume(p);
setJob(j.Job);
}
//functii getter si setter pentru datele membre
string setnume(string n){ nume=n;}
string getnume(){ return nume;}
string setprenume(string p){ prenume=p;}
string getprenume(){ return prenume;}
Profesie setJob(Profesie j){Job=j;}
Profesie getJob(){return Job;}}
persoana operator<<(ostream&);
private://datele membre
string nume;
string prenume;
Profesie Job;
};
ostream& operator<<(ostream& o, Profesie pr)
{
o << "Rol: " << pr.getRol()<<"; "
<< "Salariul: " <<pr.getSalariu();
return o;
}

ostream& operator<<(ostream& o, Persoana)
{
o<< "Nume: " << p.getnume()
<<"Prenume: "<<p.getprenume()
<< "Rol: " << pr.getRol()<<"; "
<< "Salariul: " <<pr.getSalariu();

return o;
}

int main()
{
Profesie p1("inginer", 5000);
Persoana pers1("Daniel", "Ionescu", p1);
Persoana pers2("Cosmin", "Munteanu", "arhitect", 5000);
cout << p1 << endl << endl << pers1 << endl << endl
<< pers2 << endl;
cout << endl << "***Schimbarea meseriei***" << endl;
pers1.getJob().setRol("antreprenor");
cout << pers1 << endl;
system("PAUSE");
return 0;
}

The result have to be:

Rol: inginer; Salariu: 5000 ron
Nume: Daniel; Prenume: Ionescu
Job: Rol: inginer; Salariu:5000ronNume: Cosmin; Prenume: MunteanuJob: Rol: arhitect; Salariu: 5500ron
***Schimbarea meseriei***
Nume: Daniel; Prenume: IonescuJob: Rol: antreprenor; Salariu: 5000ron
What errors, exactly?
In constructor 'Persoana::Persoana(std::string, std::string, Profesie&)':
[Error] 'class Profesie' has no member named 'Job'
In member function 'std::string Persoana::setnume(std::string)':
[Error] 'nume' was not declared in this scope
In member function 'std::string Persoana::setprenume(std::string)':
[Error] 'prenume' was not declared in this scope


and many others :(
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

In constructor 'Persoana::Persoana(std::string, std::string, Profesie&)':
[Error] 'class Profesie' has no member named 'Job'


Indeed. There seems to be just 'rol' and 'salariu' and even they are inaccessible:
1
2
3
4
5
6
class Profesie
{
private:
  string rol;
  int salariu;
};


Where does the error happen?
1
2
3
4
5
6
Persoana::Persoana( string n, string p, Profesie& foo )
{
  setnume( n );
  setprenume( p );
  setJob( foo.Job ); // error
}

What does the 'setJob' expect?
1
2
3
4
Profesie Persoana::setJob( Profesie bar )
{
  Job = bar;
}

IF foo is a Profesie AND setJob expects a Profesie
THEN why do you try to offer member "Job" of foo to setJob?

Overall, why do you call function in the body of constructor?
Why do you do anything in the body of the constructor?

Use the member initializer list syntax:
1
2
3
4
Persoana::Persoana( string n, string p, const Profesie& foo )
  : nume( n ), prenume( p ), Job( foo )
{
}

Thanks for your help. I am new to c++ Classes that's why i don't know how constructors works. So I need 2 constructors because of the main
1
2
Persoana pers1("Daniel", "Ionescu", p1);
Persoana pers2("Cosmin", "Munteanu", "arhitect", 5000);


My class should be like this?
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
class Persoana
{
public:
     //constructor
	Persoana( string n, string p, const Profesie& foo ) : nume( n ), prenume( p ), Job( foo )
{
	
}
	 Persoana(string n, string p, Profesie& foo)
	 {
	 	setnume(n);
	 	setprenume(p);
	 	
	 	
	 }
     //functii getter si setter pentru datele membre
     string setnume(string n){ nume=n;}
     string getnume(){ return nume;}
     string setprenume(string p){ prenume=p;}
     string getprenume(){ return prenume;}
     persoana operator<<(ostream&);
private://datele membre
    string nume;
	string prenume;
	Profesie Job;
};

Profesie Persoana::setJob( Profesie bar )
{
  Job = bar;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ostream& operator<<(ostream& o, Profesie pr)
{
      o << "Rol: " << pr.getRol()<<"; "
       << "Salariul: " <<pr.getSalariu();
    return o;	
}

ostream& operator<<(ostream& o, Persoana p)
{  
         o<< "Nume: " << p.getnume()
		 <<"Prenume: "<<p.getprenume();
	

    return o;
}
Last edited on
Topic archived. No new replies allowed.