Cześć
Polecenie które mam wykonać:
Ćw 2. The code below shows a class TPerson.
class TPerson { public: TPerson(string n, string s, int a) { name = n; surname = s; age = a; } int age; void DisplayPersonData( ) { cout << ”Name: ” << name << endl; cout << ”Surname: ” << surname <<endl; cout << ”Age: ” << age << endl; } protected: string name; private: string surname; };
a) Define its subclass TEmployee that inherits publicly all methods and fields from the
superclass. Add to the TEmployee class the following new elements:
- private string field company
- protected int field salary
- public methods:
void SetSalary(int s) ; // sets salary double GetSalary( ); // returns salary
- override inherited DisplayPersonData()method to display all TEmployee fields.
- Write class constructor, which takes four parameters and initializes the fields: name, surname, age and company. Use initialization list to invoke parameterized constructor from the base class.cpp
b) In the main( ) function insert the following code:
TEmployee manager(”Jan”,”Kowalski”, 30, ”OCP”); TEmployee doctor(”Eva”,”Jankowiak”, 30, ”OCP”); manager.SetSalary(3000); doctor.SetSalary(4000); doctor.DisplayPersonData();
Mój kod :
#include <iostream>
using namespace std;
class TPerson
{
public:
TPerson(string n, string s, int a)
{
name = n;
surname = s;
age = a;
}
int age;
void DisplayPersonData( )
{
cout << "Name: " << name << endl;
cout << "Surname: " << surname <<endl;
cout << "Age: "<< age << endl;
}
protected:
string name;
private:
string surname;
};
class TEmployee:public TPerson
{
private:
string company;
protected:
int salary;
public:
TPerson::TEmployee(string na, string su, int ag)
{
namee = na;
surnamee = su;
agee = ag;
}**
void DisplayEmployee**Data( )
{
cout << "Name: " << namee << endl;
cout << "Surname: " << surnamee <<endl;
cout << "Age: "<< agee << endl;
}
void SetSalary(int s);
double GetSalary(double sal);
};
int main()
{
TPerson ziom("123", "123", 3);
ziom.DisplayPersonData();
TEmployee manager("Jan","Kowalski", 30, "OCP");
TEmployee doctor("Eva","Jankowiak", 30, "OCP");
manager.SetSalary(3000);
doctor.SetSalary(4000);
doctor.DisplayPersonData();
return 0;
}
Mógłby ktoś mi z nim pomóc ?
Od niego zależy ocena na koniec roku moja i moich kumpli.
Pozdro