Solved

Inheritance Problems

Posted on 2005-04-22
Medium Priority
361 Views
Hey everyone, im having a dabble at inheritance. Ive got my two classes and now i need to test my derived class, but nothing is working even when i use the default constructor for it.

#include <iostream>
using namespace std;

class Date {

public:
Date(int, int, int);
void setDate( int d, int m, int y);
bool isLeapYear();
void printDate() const;
void setDay( int d );
void setMonth( int m );
void setYear( int y);
int getDay() const;
int getMonth() const;
int getYear() const;
bool operator== (Date& somedate);
bool operator> (Date& somedate);

private:
int day;
int month;
int year;

};

Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}

void Date::setDate( int d, int m, int y){

day = d;
month = m;
year = y;

}

bool Date::isLeapYear()
{
return ( (year % 4) ? false : true);
}

void Date::printDate() const
{
cout << day << ", "<<  month << ", " <<year;

}

void Date::setDay( int d )
{
day = d;
}

void Date::setMonth( int m )
{
month = m;
}

void Date::setYear( int y )
{
year = y;
}

int Date::getDay() const
{
return day;
}

int Date::getMonth() const
{
return month;
}

int Date::getYear() const
{
return year;
}

bool Date::operator== (Date& somedate)
{
if (somedate.day == day &&  somedate.month == month && somedate.year == year)
return true;
else
return false;
}

bool Date::operator> (Date& somedate)
{
if (somedate.day > day &&  somedate.month > month && somedate.year > year)
return true;
else
return false;
}

class Student{
public:
Student(char * surname, Date dob);
void printStudent();
char* getName();
Date getBirthDate();
int calculateAge(int currentYear);
protected:
char sname[10];
Date birthDate;
};

Student::Student(char * surname, Date dob):birthDate(dob)
{
birthDate.setDay(dob.getDay());
birthDate.setMonth(dob.getMonth());
birthDate.setYear(dob.getYear());

strcpy(sname,surname);

}//End constructor

void Student::printStudent(){

cout << "The student named " << sname << " was born on ";
birthDate.printDate();

}//End printStudent

char * Student::getName(){

return sname;

}//End getName

Date Student::getBirthDate(){

return birthDate;

}//End getBirthDate

int Student::calculateAge(int currentYear){

return currentYear-birthDate.getYear();
}

class PartTimeStud:protected Student{
public:
PartTimeStud(char * surname, Date dob, float coursefee);
double getfees();
void printStudentInfo();
private:
double fees;
};

void PartTimeStud::printStudentInfo(){
cout << "The student name: " << sname << " was born on ";
birthDate.printDate();
}

double PartTimeStud::getfees(){
return fees;
}

int main()
{

return 0;
}

I also need to change the print info statement to print out the fees aswell

David
0
Question by:David_4321
• 2

LVL 86

Accepted Solution

jkr earned 750 total points
ID: 13844341

class PartTimeStud:public Student{
public:
PartTimeStud(char * surname, Date dob, float coursefee) : Student(surname, dob), fees(coursefee) {}
double getfees();
void printStudentInfo();
private:
double fees;
};

void PartTimeStud::printStudentInfo(){
cout << "The student name: " << sname << " was born on ";
birthDate.printDate();
}

double PartTimeStud::getfees(){
return fees;
}

int main()
{
Date dob ( 10, 10, 2005);
PartTimeStud pts("test", dob, 9.99);

return 0;
}
0

Author Comment

ID: 13844530

I can get that to compile and run however i cannot seem to get any functions working like        getfees, getname etc
0

LVL 86

Expert Comment

ID: 13844579
That might be because they're not being called:

int main()
{
Date dob ( 10, 10, 2005);
PartTimeStud pts("test", dob, 9.99);
cout << pts.getfees() << endl;

return 0;
}

outputs '9.99'
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. â€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month9 days, 8 hours left to enroll