Описание каждого из классов размещаем в отдельном заголовочном файле с расширением «*.h».
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!student.h!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <stdio.h>
class CStudent
{protected:
int course;
float sr_ball;
char fio[30];
public:
CStudent()
{course = 1;
sr_ball = 0;
}
void input()
{printf("Vvedite course, sr. ball and fio\n");
scanf("%i %f %s", &course, &sr_ball, fio);
}
void print_data()
{printf("Course = %i, sr. ball = %5.1f and fio = %s\n", course, sr_ball, fio);
}
};// закрываем оператор объявления класса
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!good.h!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <stdio.h>
class CGood_Student: public CStudent
{protected:
float stip;
public:
CGood_Student ()
{stip = 0;
}
void give_stip()
{if (sr_ball>=4) stip = 10;
if (sr_ball == 5) stip = 20;
}
void print_stip()
{printf ("Stipendia = %7.2f\n", stip);
}
};// закрываем оператор объявления класса
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!bad.h!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <stdio.h>
class CBad_Student: public CStudent
{protected:
int dolg;
public:
CBad_Student()
{dolg = 1;
}
void input()
{printf("\nInput from CBad_Student\nVvedite course, sr. ball, kol-vo dolgov and fio\n");
scanf("%i %f %i", &course, &sr_ball,&dolg);
getchar(fio);// считывание из потока символа перехода на новую строку,
//введенного с клавиатуры в предыдущем операторе
gets(fio);
}
void print_data()
{printf("\nOutput from CBad_Student\Course = %i, sr. ball = %5.1f, dolgov = %i and fio = %s\n", course, sr_ball, dolg, fio);
}
};// закрываем оператор объявления класса
Создадим файл для работы с объектами разработанных классов CStudent, CGood_Student и CBad_Student.
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!nasledovanie.cpp!!!!!!!!!!!!!!!!!!!!!!!!
#include <stdio.h>
#include "student.h"
#include "good.h"
#include "bad.h"
void main()
{ printf("\n!!!!!!!!!!!!!!!!!Class CStudent!!!!!!!!!!!!!!!!!!!!!\n");
CStudent st1;
st1.print_data();// полиморфизм
st1.input();// полиморфизм
st1.print_data();// полиморфизм
printf("\n!!!!!!!!!!!!!!!Class CGood_Student!!!!!!!!!!!!!!!!!!\n");
CGood_Student st2;
st2.input();//наследование, полиморфизм
st2.print_data();//наследование, полиморфизм
st2.give_stip();
st2.print_stip();
printf("\n!!!!!!!!!!!!!!!Class CBad_Student!!!!!!!!!!!!!!!!!!!\n");
CBad_Student st3;
st3.input();// полиморфизм
st3.print_data();// полиморфизм
}