Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <math.h>
- using namespace std;
- class Shape{
- protected:
- int strana;
- public:
- Shape(){
- strana=0;
- }
- Shape(int strana1){
- strana=strana1;
- }
- virtual double plostina()=0;
- virtual void pecati()=0;
- virtual int getType()=0;
- };
- class Square:public Shape{
- public:
- Square(){
- }
- Square(int strana1):Shape(strana1){
- }
- double plostina(){
- return strana*strana;
- }
- void pecati(){
- cout<<"Square so strana "<<strana<<" i polostina: "<<plostina()<<endl;
- }
- int getType(){
- return 1;
- }
- };
- class Circle:public Shape{
- public:
- Circle(){
- }
- Circle(int strana1):Shape(strana1){
- }
- double plostina(){
- return 3.14*strana*strana;
- }
- void pecati(){
- cout<<"Circle so strana "<<strana<<" i polostina: "<<plostina()<<endl;
- }
- int getType(){
- return 2;
- }
- };
- class Triangle:public Shape{
- public:
- Triangle(){
- }
- Triangle(int strana1):Shape(strana1){
- }
- double plostina(){
- return (sqrt(3)/4)*strana*strana;
- }
- void pecati(){
- cout<<"Triangle so strana "<<strana<<" i polostina: "<<plostina()<<endl;
- }
- int getType(){
- return 3;
- }
- };
- void checkNumTypes(Shape **niza,int n){
- int brojacSquare=0;
- int brojacCircle=0;
- int brojacTriangle=0;
- for(int i=0;i<n;i++){
- if(dynamic_cast<Square*>(niza[i])!=0){
- brojacSquare++;
- }else if(dynamic_cast<Circle*>(niza[i])!=0){
- brojacCircle++;
- }else if(dynamic_cast<Triangle*>(niza[i])!=0){
- brojacTriangle++;
- }
- }
- cout<<"Kvadrati: "<<brojacSquare<<endl;
- cout<<"Krugovi: "<<brojacCircle<<endl;
- cout<<"Triagolnici: "<<brojacTriangle<<endl;
- }
- int main(){
- int n;
- cin >> n;
- //TODO: inicijaliziraj niza od pokazuvachi kon klasata Shape
- Shape **niza;
- //TODO: alociraj memorija so golemina n za prethodno navedenata niza
- niza=new Shape*[50];
- int classType;
- int side;
- //TODO: konstruiraj for ciklus so chija pomosh ke ja popolnish nizata
- for(int i = 0; i < n; ++i){
- cin >> classType;
- cin >> side;
- if(classType==1)
- niza[i]=new Square(side);
- if(classType==2)
- niza[i]=new Circle(side);
- if(classType==3)
- niza[i]=new Triangle(side);
- }
- for(int i = 0; i < n; ++i){
- niza[i]->pecati();
- }
- checkNumTypes(niza, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement