Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

12пми / shapeVirtual

.cpp
Скачиваний:
17
Добавлен:
02.06.2015
Размер:
3.65 Кб
Скачать
// ConsoleApplication13.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <vector>
#include <string>

using namespace std;

class Shape
{
protected:
	double height, width;//Height Width
	
public:
	string name;// название фигуры
	Shape(): height(0), width (0),name("null"){}
	Shape (double h, double w,string n) {height=h, width=w; name=n;} 
	
	void input_hw()
	{
		cout<<"write heght"<<endl;
		cin>>height;
		cout<<"write width"<<endl;
		cin>>width;
	}
	void show_hw()
	{
		cout<<"height="<<height<<endl;
		cout<<"width="<<width<<endl;
	}
	//virtual void show_area() = 0; // чисто виртуальная
	 virtual double getArea() 
	 {
	  return 0.0;
	 }
	
};

class Triangle: public Shape
{
public:
	Triangle():Shape(5,3,"tri"){}// конструктор
	Triangle (double h, double w,string n): Shape(h,w,n) {name="tri";} 
	double getArea()
	{
		return ((height*width)/2);
	}
};

class Rectangle: public Shape
{
public:
	Rectangle():Shape(5,2,"rect"){}
	Rectangle (double h, double w,string n): Shape(h,w,n)
	{
		if (height==width) {name="square";}
		else{name="rectangel";}
	}
	
	double getArea()
	{
		return (height*width);
	}
	
};

class Ellipse: public Shape 
{
	public:
		Ellipse (double h, double w,string n): Shape(h,w,n) { if (height==width) {name="circle";}else{name="ellips";}}
	Ellipse (): Shape(5,8,"ell") {}

	double getArea()
	{
		return (3.14*(height/2)*(width/2));
	}
};

int main()
{
	Shape* shapes [6];
    shapes[0] = new Triangle(5,7,"");
    shapes[1] = new Rectangle(10,10,"");
    shapes[2] = new Rectangle(10, 4,"");
    shapes[3] = new Ellipse(3,3,"");
    shapes[4] = new Ellipse(3,9,"");
    shapes[5] = new Shape(10, 20, "shape");

    for (int i = 0; i < 6 ; i++)
    {
        cout<< "This is " << shapes[i]->name<< ",";
        cout<< " area =  " << shapes[i]->getArea() << endl;
    }
	
	for (int i = 0; i < 6; ++i)
	{
		delete shapes[i];
	}
	   
	return 0;
}
	/*
	Triangle tr(5,5);
	Rectangle rct(3,3);
	Ellipse ell(2,2);
	Triangle tr1();
	tr.input_hw();
	tr.show_hw();
	// работает 
	cout<<" S tr= "<<tr.getArea()<< endl;
	cout<<" S rct= "<<rct.getArea()<< endl;
	cout<<" S ell= "<<ell.getArea()<< endl;
	// доступ через указатели
	//Shape*p=&tr;
	//cout<<"area of tr="<<p->getArea()<<endl;
	//p=&rct;
	//cout<<"area of rect="<<p->getArea()<<endl;

	//shapes* p1 = &tr;

	vector<Shape *> shapes;// вектор фигур
	for (int i = 0; i < 6; i+=3)
	{
	/*	Shape * sh1;
		sh1 = new Triangle();
		
		Triangle tr1;
		Rectangle rct1;
		Ellipse ell1;
		shapes.push_back(tr1);
		shapes.push_back(rct1);
		shapes.push_back(ell1);

		Shape * sh1;
		sh1 = new Triangle();
		shapes.push_back(sh1);// push_back(sh1);
		sh1 = new Rectangle();
		shapes.push_back(sh1);
		sh1 = new Ellipse();
		shapes.push_back(sh1);
	}
	 for (int i = 0; i < shapes.size(); ++i)
	 {
	  shapes[i]->input_hw();
	 }

	 for (int i = 0; i < shapes.size(); ++i)
	 {
	  shapes[i]->show_hw();
	 }
 
	 for (int i = 0; i < shapes.size(); ++i)
	 {
	  cout <<"area of shape "<< i <<" = "<<shapes[i]->getArea()<< endl;
	 }
	 
	 for (int i = 0; i < shapes.size(); ++i)
		 {
			delete shapes[i];
		 }
	
	
	/*for (int i = 0; i < shapes.size(); ++i)
	{
		shapes[i].input_hw();
	}

	for (int i = 0; i < shapes.size(); ++i)
	{
		shapes[i].show_hw();
	}
	// площадь 0 - метод из наследников не вызывается.вызывается  getArea() только из Shape
	for (int i = 0; i < shapes.size(); ++i)
	{
		cout << shapes[i].getArea();
	}*/

	
	
Соседние файлы в папке 12пми