-
C++ Derived classes
Sit and tinker with that derived classes and I have encountered some problems..
Here are the classes:
Code:
Class Person (
public:
Person (const string & n): name (n) ();
const string & units () const;
void andra_namn (const string & n);
void skriv_info () const;
private:
string name;
);
class Course;
class Student: public Person (
public:
Student (const string & n): Person (n) ();
void borja_kurs (Course & k);
void sluta_kurs (Course & k);
void skriv_info () const;
private:
vector <Kurs *> courses;
);
class Course (
public:
Course (const string & name): knamn (name) ();
const string & course name () (return knamn;)
void ny_student (Student & s);
void student_bort (Student & s);
void exit ();
void skriv_info () const;
public:
string knamn;
vector <Student *> stud;
);
And here are some features:
Code:
const string & Privacy:: units () const
(Return name;)
void Person:: skriv_info () const
(Court << "Name:" <<name <<Endla;)
void Student:: borja_kurs (Course & k)
(
for (int i = 0; i <kurser.size (); i + +)
if (rates [i] == & k)
return;
kurser.push_back (& k);
k.ny_student (* this);
)
But how do you do then to call on these features inside the main?
Help appreciated!
-
Re: C++ Derived classes
Try this code,
Code:
int main ()
(
Personal sven ( "Sven");
sven.skriv_info ();
return 0;
)
-
Re: C++ Derived classes
If you plan to use the polymorphism it gets a little trickier, but to say I can (or some other safe) try to explain.
-
Re: C++ Derived classes
Code:
string first name;
string Lastname;
Court << "Your first name:";
cin>> first name;
Court << "Your Surname:";
cin>> last name;
Person's name (first name + "" + last name);
namn.skriv_info ();
Is this the easiest way for users to insert something into a class
or is there something better?
Thank you for taking the time.
-
Re: C++ Derived classes
Better / easier is a bit of a matter of taste and situation dependent. Want to change the name after you created the item? Then you need a set function. Otherwise, if it is not needed, or if you absolutely do not want it to change in circumstances, it is best to handle it in.
Code:
string name;
std:: Court << "Name:";
std:: cin>> name;
/ / Alt 1:
Person obj = Person (name);
/ / Alt 2:
Person obj;
obj.setName (name);
Pick your poison.
Page generated in 1,717,387,965.01661 seconds with 10 queries