C++ (Qt)/*virtual*/ double Rectangle::calcPerimeter(){ mPerimeter = 2.0 * (mHeight + mWidth); return mPerimeter;} /*virtual*/ double Rectangle::calcArea(){ mArea = mHeight * mWidth; return mArea;}
C++ (Qt)#ifndef SHAPE_H#define SHAPE_H class Shape{public: Shape(); virtual ~Shape(); virtual double calcPerimeter() = 0; virtual double calcArea() = 0; double perimeter() const; double area() const; protected: double mPerimeter; double mArea;}; #endif // SHAPE_H
C++ (Qt)#include <QCoreApplication>#include <QTextStream>#include "circle.h"#include "rectangle.h" QTextStream cin(stdin);QTextStream cout(stdout); int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Circle *pc = new Circle; double radius = 5.5; cout << endl; cout << "Circle:" << endl; cout << "Radius = " << radius << endl; cout << "Perimeter = " << pc->perimeter(radius) << endl; cout << "Area = " << pc->area(radius) << endl; cout.flush(); delete pc; Rectangle *pr = new Rectangle; double height = 5.0; double width = 10.0; cout << endl; cout << "Rectangle:" << endl; cout << "Height = " << height << endl; cout << "Width = " << width << endl; cout << "Perimeter = " << pr->perimeter(height, width) << endl; cout << "Area = " << pr->area(height, width) << endl; cout.flush(); delete pr; return a.exec();}
C++ (Qt)#ifndef CIRCLE_H#define CIRCLE_H #include <cmath> class Circle{public: inline double perimeter(double radius) const { return (2.0 * M_PI * radius); } inline double area(double radius) const { return (M_PI * radius * radius); }}; #endif // CIRCLE_H
C++ (Qt)#ifndef RECTANGLE_H#define RECTANGLE_H class Rectangle{public: inline double perimeter(double height, double width) const { return 2.0 * (height + width); } inline double area(double height, double width) const { return height * width; }}; #endif // RECTANGLE_H
C++ (Qt)setName("test");
C++ (Qt)#include <QCoreApplication>#include <QTextStream> QTextStream cin(stdin);QTextStream cout(stdout); class Person {public: void setName(const QString& name) { mName = name; } QString name() const { return mName; }private: QString mName;}; int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Person ivan("Ivan"); return a.exec();}
C++ (Qt)class Circle{public: inline double perimeter(double radius) const { return (2.0 * M_PI * radius);
C++ (Qt)class Circle : public Shape{public:... static double perimeter(double radius) { return (2.0 * M_PI * radius); } virtual double perimeter( void ) const { return perimeter(radius); } private: double m_radius;
C++ (Qt)class Shape{public: virtual ~Shape() {} virtual double perimeter() const = 0; virtual double area() const = 0;};
C++ (Qt)class Circle : public Shape{public: explicit Circle( double radius ) : m_radius( radius ) { } double perimeter() const { return (2.0 * M_PI * m_radius); } double area() const { return (M_PI * m_radius * m_radius); } private: double m_radius;};