Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2014 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0eedd7719a34655488fb to your computer and use it in GitHub Desktop.
Save anonymous/0eedd7719a34655488fb to your computer and use it in GitHub Desktop.
C++ shape code
#include "circle.h"
#include "shape.h"
#include <QString>
#include <cmath>
#include <QDebug>
using namespace std;
class circle: public shape
{
double circle::area()
{
return(3.14*radius*radius);
}
QString getName()
{
return name;
}
QString getDimensions()
{
return dimensions;
}
};
#ifndef CIRCLE_H
#define CIRCLE_H
#include <QString>
#include <QDebug>
class circle: public shape
{
public:
circle(double r): radius(r)
{}
double area();
QString getName();
QString getDimensions();
private:
double radius;
};
#endif
#include <QtCore/QCoreApplication>
#include "shape.h"
#include "circle.h"
#include "rectangle.h"
#include "square.h"
#include <QString>
#include <iostream>
#include <cmath>
#include <QDebug>
using namespace std;
void showNameAndArea (shape * pshp)
{
qDebug()<<pshp->getName()
<< " " << pshp->getDimensions()
<<" area= " << pshp->area();
}
int main()
{
rectangle rec(4.1,5.2);
square squ(5.1);
circle cir(6.1);
qDebug() << "this program uses hierarchies for shapes";
showNameAndArea(&rec);
showNameAndArea(&cir);
showNameAndArea(&squ);
return 0;
};
#include "rectangle.h"
#include"shape.h"
#include <QString>
#include <cmath>
#include <QDebug>
using namespace std;
class rectangle: public shape
{
double Rectangle::area()
{
return(height*width);
}
QString getName()
{
return name;
}
QString getDimensions()
{
return dimensions;
}
};
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <QString>
#include <QDebug>
class rectangle: public shape
{
public:
rectangle(double h, double w) :height(h),width(w)
{}
double area();
QString getName();
QString getDimensions();
protected:
double height, width;
};
#endif
#include "shape.h"
#include "circle.h"
#include "rectangle.h"
#include "square.h"
#include <QDebug>
#include <QString>
#include <iostream>
using namespace std;
shape::shape()
{
};
#ifndef SHAPE_H
#define SHAPE_H
#include <QString>
#include <QDebug>
using namespace std;
class shape
{
public:
virtual double area()=0;
virtual QString getName()=0;
virtual QString getDimensions()=0;
virtual~shape(){}
};
#endif
#include "square.h"
#include "rectangle.h"
#include "shape.h"
#include <string>
#include <cmath>
#include <QDebug>
#include <QString>
using namespace std;
class square: public rectangle
{
double square::area()
{
return(rectangle::area());
}
QString getName()
{
return name;
}
QString getDimensions()
{
return dimensions;
}
};
#ifndef SQUARE_H
#define SQUARE_H
#include <QString>
#include <QDebug>
class square: public rectangle
{
public:
square(double h): rectangle(h,h)
{}
double area();
QString getName();
QString getDimensions();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment