Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- static const double PI = 3.14;
- /* BEGIN Zadanie 1 */
- class Shape{
- private:
- string name;
- public:
- Shape(string name) : name(name){
- cout << "Tworze ksztalt" << endl;
- }
- virtual ~Shape(){
- cout << "Usuwam ksztalt" << endl;
- }
- virtual void setName(string name){
- }
- virtual string getName(){
- return name;
- }
- virtual double area(){
- return 0;
- }
- };
- class Rectangle : public Shape{
- private:
- double width;
- double height;
- public:
- Rectangle(string name, double width, double height) : Shape(name), width(width), height(height){
- cout << "Tworze prostokat" << endl;
- }
- ~Rectangle(){
- cout << "Usuwam prostokat" << endl;
- }
- double area(){
- return width*height;
- }
- };
- class Square : protected Shape{
- private:
- double width;
- public:
- Square(string name, double width) : Shape(name), width(width){
- cout << "Tworze kwadrat" << endl;
- }
- ~Square(){
- cout << "Usuwam kwadrat" << endl;
- }
- double area(){
- return width*width;
- }
- };
- void zadanie1(){
- Rectangle prost("prst1", 5.0, 6.2);
- Square kwadr("kwadr1", 3.0);
- Shape &prostRef = prost; //rzutowanie przez referencje
- Shape &kwadrRef = kwadr; //rzutowanie przez referencje
- cout << endl;
- cout << "Rzutowanie przez referencje: " << endl;
- cout << endl;
- cout << "Nazwa prostokatu: " << prostRef.getName() << ", jego pole: " <<prostRef.area() << endl;
- cout << "Nazwa kwadratu: " << kwadrRef.getName() << ", jego pole: " <<kwadrRef.area() << endl;
- cout << endl;
- Shape *prostPtr = &prost;
- Shape *kwadrPtr = &kwadr;
- cout << endl;
- cout << "Rzutowanie przez referencje: " << endl;
- cout << endl;
- cout << "Nazwa prostokatu: " << prostPtr -> getName() << ", jego pole: " <<prostPtr -> area() << endl;
- cout << "Nazwa kwadratu: " << kwadrPtr -> getName() << ", jego pole: " <<kwadrPtr -> area() << endl;
- cout << endl;
- delete prostPtr;
- delete kwadrPtr;
- }
- /* END Zadanie 1 */
- /* BEGIN Zadanie 2 */
- class Circle{
- private:
- double radius;
- public:
- Circle(double radius) : radius(radius){}
- ~Circle(){}
- virtual double area(){
- return (PI*radius*radius);
- }
- double getRadius(){
- return radius;
- }
- };
- class Cylinder : public Circle{
- private:
- double height;
- public:
- Cylinder(double radius, double height) : Circle(radius), height(height){}
- double area(){
- return ((2*PI*getRadius()*getRadius()) + (2*PI*getRadius()*height));
- }
- double volume(){
- return Circle::area() * height;
- }
- };
- void zadanie2(){
- Cylinder walec(5, 10); //przykladowy obiekt walca z r=5, h=10
- cout << "Pole powierzchni walca o r=5 i h=10: " << walec.area()<<". Objetosc tego walca wynosi: "<<walec.volume()<<endl;
- }
- /* END Zadanie 2 */
- /* BEGIN Zadanie 3 */
- class Funkcja{
- public:
- Funkcja(){}
- virtual double oblicz(float x){
- }
- };
- class FunkcjaLiniowa : public Funkcja{
- private:
- double a;
- double b;
- public:
- FunkcjaLiniowa(double a, double b) : a(a), b(b){}
- double oblicz(float x){
- return a*x+b;
- }
- };
- void zadanie3(){
- FunkcjaLiniowa f1(5,6); //przykladowa funkcja a=5, b=6
- cout << "Wartosc funkcji 5x+6 dla x=2: "<< f1.oblicz(2) << endl;
- }
- /* END Zadanie 3 */
- int main(){
- int task;
- bool stop = false;
- do{
- cout<<endl;
- cout<<"#MENU#"<<endl;
- cout << "#Ktore zadanie?(1, 2, 3) " << endl;
- cout << "#Numer 4 by zakonczyc program " << endl;
- cin>>task;
- switch(task){
- case 1:
- zadanie1();
- break;
- case 2:
- zadanie2();
- break;
- case 3:
- zadanie3();
- break;
- case 4:
- stop = true;
- break;
- default: cout << "Zly numer zadania";
- }
- } while (!stop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement