Hi
I'm trying to learn multilevel inheritance in c++ i write a following code:
code :
#include<iostream>
using namespace std;
class student
{
protected : char *a ;
protected : int age ;
public : student( char *b ,int d)
{ a =b ;
age=d;
}
};
class marks : public student
{
protected : int mark;
public : void getmarks(int x)
{
mark= x;
}
};
class info : public marks
{
public: void show()
{ cout << "\nthe name of student is = "<<a <<"\n";
cout <<"\nthe age of student is = "<<age <<"\n";
getmarks(50);
cout << "\n the marks obtain by student is = "<< mark<<"\n";
}
};
int main()
{
student s("xyz" ,25);
info i;
i.show();
}
but when i compile it it shows following errors :
i compile it with g++ mli.cpp command errors are following :
errors :
mli.cpp: In function ‘int main()’:
mli.cpp:36: warning: deprecated conversion from string constant to ‘char*’
mli.cpp: In constructor ‘marks::marks()’:
mli.cpp:15: error: no matching function for call to ‘student::student()’
mli.cpp:8: note: candidates are: student::student(char*, int)
mli.cpp:5: note: student::student(const student&)
mli.cpp: In constructor ‘info::info()’:
mli.cpp:25: note: synthesized method ‘marks::marks()’ first required here
mli.cpp: In function ‘int main()’:
mli.cpp:37: note: synthesized method ‘info::info()’ first required here
now my question are following :
1: what is wrong with the code ?
2: why are these error coming ?
3: what is the meaning of these errors ?
please explain me with example . I'm doing it for learning .
Constructor of a derived class calls a constructor of its parent. When you give a class some constructor, the default constructor is no longer supplied. Thus, "marks" tries to call the constructor of "student". Since you don't specify what constructor to call and there is no default available, it complains. The way to call parent's constructor is initializer lists. http://www.cprogramming.com/tutorial/initialization-lists-c++.html