Kilka osób prosiło więc wrzucam gotowca.
PKW.cpp
// PKW.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Punkt.h" #include "Kolo.h" #include "Walec.h" int _tmain(int argc, _TCHAR* argv[]) { double R = 5; double h = 10; Walec W(0,0,R,h); double poleW = W.Pole(); cout << "Pole walca o R = " << R << " i h = " << h << " wynosi: \n"; cout << "Obliczenie bezpośrednio w klasie walec: " << poleW << "\n"; Punkt *q=&W; double poleQ = q->Pole(); cout << "Obliczenie za pomocąfunkcji polimorficznej: " << poleW << "\n"; return 0; }
Punkt.h
#pragma once class Punkt { public: Punkt(void); Punkt(double x,double y):x(x),y(y){} virtual ~Punkt(void); protected: // wspolrzedna x double x; // wspolrzedna y double y; public: // obliczenie pola virtual double Pole(void); };
Punkt.cpp
#include "StdAfx.h" #include "Punkt.h" Punkt::Punkt(void) { } Punkt::~Punkt(void) { } // obliczenie pola double Punkt::Pole(void) { return 0; }
Kolo.h
#pragma once #include "punkt.h" class Kolo : public Punkt { public: Kolo(void); Kolo(double x,double y,double R=1):Punkt(x,y),R(R) {} ~Kolo(void); protected: // promien kola double R; public: // obliczenie pola kola double Pole(void); };
Kolo.cpp
#include "StdAfx.h" #include "Kolo.h" Kolo::Kolo(void) : R(0) { } Kolo::~Kolo(void) { } // obliczenie pola kola double Kolo::Pole(void) { return M_PI*R*R; }
Walec.h
#pragma once #include "kolo.h" class Walec : public Kolo { public: Walec(void); Walec(double x,double y,double R,double h):Kolo(x,y,R),h(h){} ~Walec(void); protected: // wysokosc walca double h; public: // pole powierzchni walca double Pole(void); };
Walce.cpp
#include "StdAfx.h" #include "Walec.h" Walec::Walec(void) : h(0) { } Walec::~Walec(void) { } // pole powierzchni walca double Walec::Pole(void) { return 2*Kolo::Pole() + 2*M_PI*R*h; }
stdafx.h
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <iostream> using namespace std; #define _USE_MATH_DEFINES #include <cmath> // TODO: reference additional headers your program requires here
stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes // PKW.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" // TODO: reference any additional headers you need in STDAFX.H // and not in this file
Offline