Hi, when i run this program the total salary displayed ( in function void displa
ID: 3633755 • Letter: H
Question
Hi, when i run this program the total salary displayed ( in function void displaysalarie() ) always shows a garbage value. I have passed derived class salaries to base class through constructors and then used a running sum to add them all up in the base class, there is something wrong in the coding or the logic,apparently.Please fix this, but do not use friend,virtual,or arrow notation. The base class must have a method to show total salary of all derived class employees! Thanks!
#include<iostream>
#include<string>
using namespace std;
//Base class
class Employee
{
protected:
string Name;int salarie;int sale;
int id;
public:
Employee()
{salarie=0;
}
Employee(string Nam,int iden, int sala)
{
Name=Nam;
id =iden;
sale=sala; salarie +=sale;
}
void display()
{
cout<<" Professor Name "<<Name <<endl;
cout<<"Professor Id "<<id <<endl;
//salarie += sale;
}
void displaysalarie()
{
cout<<"Total salary combined is "<<salarie<<endl;
}
//Destructor
~Employee(){cout<<"Destroying class Employee ";}
};
//Derived class Professor
class Professor:public Employee
{
string subject;
public:
Professor(){}
Professor(string Name,int id,string sub,int sa):Employee(Name,id,sa)
{
subject =sub;
}
void putdata()
{
display();
cout<<" Subject "<<subject <<endl;
}
~Professor(){cout<<"Destroying class Professor ";}
};
class TA:public Professor
{
string names;
public:
TA(){}
TA(string Name,int id,string subject,string name,int salaryy):Professor(Name,id,subject,salaryy)
{
names =name;
}
void disp()
{
putdata();
cout<<" Techincal Assistant Name"<<names;
}
~TA(){cout<<"Destroying class TA ";}
};
class AssProfessor:public Employee
{
int salary;
public:
AssProfessor(string Name,int id,int salari):Employee(Name,id,salari)
{
salary =salari;
}
void print()
{
display();
cout<<" Salary "<<salary <<endl;
}
~AssProfessor(){cout<<"Destroying class AssProfessor ";}
};
class Lectures:public Employee
{
int salary;
public:
Lectures(string Name,int id,int salar):Employee(Name,id,salar)
{
salary =salar;
}
void print()
{
display();
cout<<" Salary "<<salary <<endl;
}
~Lectures(){cout<<"Destroying class Lectures ";}
};
void main()
{
Professor pro("Mark",334,"Database",100);
pro.putdata ();
TA ta("Martin",933,"Mathematics","Zion",100);
ta.disp();
AssProfessor ap("Raj",3232,100);
ap.print();
Lectures lt("Luther",3434,100);
lt.print();
Employee t("Emma",369,100);
t.displaysalarie();
Explanation / Answer
See the error is that the salarie variable should not be a simple int variable, it should be static variable.So declare as shown below. Also if ur making any variable static then it must be initialized with zero outside the class. So at the end of Employee class just write----- int Employee::salarie=0;
class Employee
{
protected:
string Name;int salarie;int sale; --------------------------------> Make the salarie as static
write" static int salarie"
int id;
public:
Employee()
{salarie=0;
}
Employee(string Nam,int iden, int sala)
{
Name=Nam;
id =iden;
sale=sala; salarie +=sale;
}
void display()
{
cout<<" Professor Name "<cout<<"Professor Id "<//salarie += sale;
}
void displaysalarie()
{
cout<<"Total salary combined is "<
}
//Destructor
~Employee(){cout<<"Destroying class Employee ";}
};
int Employee::salarie=0;---------------------------------------------->Initialization of static variable is done aoutsie the class .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.