Created
December 11, 2014 01:39
-
-
Save anonymous/0eedd7719a34655488fb to your computer and use it in GitHub Desktop.
C++ shape code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "shape.h" | |
#include "circle.h" | |
#include "rectangle.h" | |
#include "square.h" | |
#include <QDebug> | |
#include <QString> | |
#include <iostream> | |
using namespace std; | |
shape::shape() | |
{ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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