Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Shape
- {
- protected:
- int a;
- public:
- Shape(int a = 0)
- {
- this->a=a;
- }
- virtual double ploshtina ()=0;
- virtual void pecati()=0;
- virtual int getType()=0;
- };
- class Square :public Shape
- {
- public:
- Square(int a = 0) :Shape(a){}
- double ploshtina()
- {
- return a*a;
- }
- void pecati()
- {
- cout<<"Kvadrat so plostina = "<<ploshtina()<<endl;
- }
- int getType()
- {
- return 1;
- }
- };
- class Circle :public Shape
- {
- public:
- Circle(int a=0): Shape(a){}
- double ploshtina()
- {
- return 3.14*a *a;
- }
- void pecati()
- {
- cout<<"Krug so plostina = "<<ploshtina()<<endl;
- }
- int getType()
- {
- return 2;
- }
- };
- class Triangle:public Shape
- {
- public:
- Triangle(int a=0) :Shape(a){}
- double ploshtina()
- {
- return (sqrt(3)/4) * a * a;
- }
- void pecati()
- {
- cout<<"Triagolnik so plostina = "<<ploshtina()<<endl;
- }
- int getType(){
- return 3;
- }
- };
- void checkNumTypes(Shape** niza, int n)
- {
- int brojac=0 , brojac1=0, brojac2=0;
- for(int i = 0 ; i < n ; i ++ )
- {
- Square *s= dynamic_cast<Square*>(niza[i]);
- if(s!=0){
- brojac++;
- }
- Circle*c=dynamic_cast<Circle*>(niza[i]);
- if(c!=0)
- {
- brojac1++;
- }
- Triangle*t=dynamic_cast<Triangle*>(niza[i]);
- if(t!=0)
- {
- brojac2++;
- }
- }
- cout<<"Broj na kvadrati vo nizata = "<<brojac<<endl;
- cout<<"Broj na krugovi vo nizata = "<<brojac1<<endl;
- cout<<"Broj na triagolnici vo nizata = "<<brojac2<<endl;
- }
- int main(){
- int n;
- cin >> n;
- Shape**niza=new Shape*[n];
- int classType;
- int side;
- for(int i = 0; i < n; ++i){
- cin >> classType;
- cin >> side;
- if(classType==1)
- {
- niza[i]=new Square(side);
- }
- else if(classType==2)
- {
- niza[i]=new Circle(side);
- }
- else if(classType==3)
- {
- niza[i]=new Triangle(side);
- }
- }
- for(int i = 0; i < n; ++i){
- niza[i]->pecati();
- }
- checkNumTypes(niza, n);
- return 0;
- }
Add Comment
Please, Sign In to add comment