I am practicing the inheritance concept od oop in c++. It seems like a simple program but the linking error my compiler Dev C++ 4.9.9.2 version is giving me, makes it look sooo annoyingly difficult. Error is:
[Linker error] undefined reference to `vtable for Cd'
[Linker error] undefined reference to `Cd::~Cd()'
Here is my code:
HEADER FILE:
#ifndef Cd_H_
#define Cd_H_
class Cd{
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(char *s1=NULL, char *s2=NULL, int n=0, double x=0.0);
Cd(const Cd & d);
virtual ~Cd(){}
virtual void Report() const;
Cd & operator=(const Cd & d);
};
class Classic:public Cd
{
char primaryworks[100];
public:
Classic(char *pw=NULL,char *s1=NULL, char *s2=NULL, int n=0, double x=0.0);
Classic(char *pw, const Cd &d);
Classic(const Classic &d);
virtual void Report()const;
Classic & operator=(const Classic &d);
};
#endif
CDMETHODS.CPP:
#include "Cd.h"
#include<iostream.h>
#include<string.h>
using namespace std;
Cd::Cd(char *s1, char *s2, int n, double x ){
strncpy(performers,s1,49);
performers[49]='\0';
strncpy(label,s1,19);
performers[19]='\0';
selections=n;
playtime=x;
}
Cd::Cd(const Cd &d){
strncpy(performers, d.performers, 49);
performers[49]='\0';
strncpy(label,d.label,19);
performers[19]='\0';
selections=d.selections;
playtime=d.playtime;
}
void Cd::Report() const{
cout<<"Cd information:"<<endl<<"Cd performers:"<<performers<<endl;
cout<<"CD label:"<<label<<"\nNumber of selections:"<<selections
<<"\nTotal minutes:"<<playtime;
}
Cd & Cd::operator=(const Cd &d){
if(this==&d)
return *this;
else{
strncpy(performers, d.performers, 49);
performers[49]='\0';
strncpy(label,d.label,19);
performers[19]='\0';
selections=d.selections;
playtime=d.playtime;
return *this;
}
}
Classic::Classic(char *pw, char* s1, char *s2, int n, double x)
:Cd(s1,s2,n,x) {
strncpy(primaryworks, pw, 99);
primaryworks[99]='\0';
}
Classic::Classic(char *pw, const Cd &d)
:Cd(d){
strncpy(primaryworks, pw, 99);
primaryworks[99]='\0';
}
Classic::Classic(const Classic &d)
:Cd(d){
strncpy(primaryworks, d.primaryworks, 99);
primaryworks[99]='\0';
}
void Classic::Report()const{
//cant directly access base class's private data
Cd::Report();
cout<<"\nPrimary works included are:"<<primaryworks;
}
Classic & Classic::operator=(const Classic &d){
if(this==&d)
return *this;
else{
Cd::operator=(d);
strncpy(primaryworks, d.primaryworks, 99);
primaryworks[99]='\0';
}
}
USECD.CPP
#include<iostream>
#include "Cd.h"
using namespace std;
void Bravo(const Cd & disk);
int main()
{
Cd c1("Beatles", "Capitol",14,35.5);
Classic c2=Classic("Piano Sonata in B FLat, Fantasia in C", "Alfred Brendal", "Philips"
,2, 57.45);
Cd *pcd=&c1;
cout<<"Using object directly:\n";
c1.Report();
c2.Report();
cout<<"Using type cd *pointer to object:\n";
pcd->Report();
pcd=&c2;
pcd->Report();
cout<<"Calling a function with a CD reference argument:\n";
Bravo(c1);
Bravo(c2);
cout<<"Testing assignment:\n";
Classic copy;
copy=c2;
copy.Report();
return 0;
}
void Bravo(const Cd &disk)
{
disk.Report();
}
I have included both cpp files in the project. Please Help, i m counting on this forum a lot!