Attached is the code I wrote, I used the library <list>.
I have got "red line" (error) under "print" function in the "main_01_05.cpp" file.
And I have no idea why?? ) -:
Waiting for your opinion ... Many thanks in advance
Harel
#ifndef st
#define st
#include <string>
#include <iostream>
usingnamespace std;
class Stu
{
protected:
Stu( string , string , string , int );
//id
string id;
//first name
string f_n;
//last name
string l_n;
//number of courses
int num;
virtualbool milga();
virtualvoid print();
}
#endif
"Stud doesn't have a public print function". -
You are right.
But I'll explain what I wanted to do.
I wanted that "Stu" will be my base class and all other classes inherit from it.
There are some functions I've defined as virtual (such as "print function").
So even though "print" is not defined in the base class as "public" is not supposed to do error.
Am I right?
" You have forgot to put semicolons after the class definitions"-
I did not understand what you mean, could you please send me the code segment which you speak?
"You don't need a semicolon after the function definitions"-
I did not understand what you mean, could you please send me the code segment which you speak?
If you don't make print() public in Stu you are not allowed to call that function through a Stu pointer even if the function is public in the other classes. If you don't want to define the function in Stu you can make it a pure virtual function. virtualvoid print() = 0;
This will forbid you to create instances of Stu and all subclasses that don't define the function. It is a good way to force subclasses to define certain functions.
1 2 3 4 5 6 7 8 9
class Foo
{
void bar();
};<-- put semicolon here!void Foo::bar()
{
} <-- No need to put semicolon here!
Just one last question:
If I want to move from one link to the next link in my List, but without deleting the link, what should I do?
Command that I used it (pop_front) delete the link in Front...
for (list<Stu*>::iterator it = all_stu.begin(); it != all_stu.end(); ++it)
{
(*it)->print();
/* The above will do the same as below. Use whatever you prefer.
Stu* stu = *it;
stu->print();
*/
}
In C++11 you can also use range-based for loops to do the same thing.
1 2 3 4
for (Stu* stu : all_stu)
{
stu->print();
}
If you are not familiar with iterators you probably should read up on it.