logo MSJO.kr

QT C++ Class 기초예제

2020-03-21
MsJ
   

Windows OS 경우 네이티브 컴파일러를 이용하여 애플리케이션을 만들 수 있는 환경은 MFC(Win32API), QT(C++) 그리고 델파이(C++Builder) 정도에 불과하다. QT는 5.15 LTS 버전을 마지막으로 QT 6.0을 올 하반기에 출시한다. 6.0은 C++17을 기반으로 하고 있으며, qmake를 빌드시스템으로 사용하지 않는다고 한다. 자세한 내용은 여기를 참고하자. 이번 글에서는 QT(C++)에서 class를 사용하는 기초 예제를 소스 형태(템플릿)로 작성하여 보았다.

main.cpp
// Qt Console Application
#include "circle.h"
#include "shape.h"
#include <QCoreApplication>
#include <iostream>
#include <memory>

using namespace std;

void ShowArea(Shape &shape);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    // [1...]
    // Shape square(10, 5);
    // Circle circle(10);
    // ShowArea(square);
    // ShowArea(circle);
    
    // [2...]
    // Shape *square = new Shape(10, 5);
    // Circle *circle = new Circle(10);
    // ShowArea(*square);
    // ShowArea(*circle);
    // delete square;
    // delete circle;
    
    // [3...]
    auto square = make_unique<Shape>(10, 5);
    auto circle = make_unique<Circle>(10);
    ShowArea(*square);
    ShowArea(*circle);

    return 0;
    // return a.exec();
}

void ShowArea(Shape &shape) { cout << "Area : " << shape.Area() << endl; }
shape.h
#ifndef SHAPE_H
#define SHAPE_H

class Shape {
  protected:
    double height;
    double width;

  public:
    static int numOfShapes;

    Shape(double lenght);
    Shape(double height, double width);
    Shape();
    virtual ~Shape();

    void SetHeight(double height);
    double GetHeight();

    void SetWidth(double width);
    double GetWidth();

    static int GetNumOfShapes();
    virtual double Area();
};

#endif // SHAPE_H
shape.cpp
#include "shape.h"
#include <iostream>

using namespace std;

int Shape::numOfShapes = 0;

Shape::Shape(double length)
{
    this->height = length;
    this->width = length;
}

Shape::Shape(double height, double width)
{
    this->height = height;
    this->width = width;
}

Shape::~Shape() { cout << "Call Shape Destructor." << endl; }

void Shape::SetHeight(double height) { this->height = height; }

double Shape::GetHeight() { return height; }

void Shape::SetWidth(double width) { this->width = width; }

double Shape::GetWidth() { return width; }

int Shape::GetNumOfShapes() { return numOfShapes; }

double Shape::Area() { return height * width; }

circle.h
#ifndef CIRCLE_H
#define CIRCLE_H

#include "shape.h"

class Circle : public Shape {
  public:
    Circle();
    Circle(double lenght);
    virtual ~Circle();
    double Area();
};

#endif // CIRCLE_H
circle.cpp
#include "circle.h"
#include <cmath>
#include <iostream>

using namespace std;

Circle::Circle(double lenght) : Shape(lenght){};

Circle::~Circle() { cout << "Call Circle Destructor." << endl; }

double Circle::Area() { return 3.141592 * pow((width / 2), 2); }
main.cpp (2)
// abstract
#include <QCoreApplication>
#include <cmath>
#include <iostream>

using namespace std;

class Shape {
  public:
    virtual double Area() = 0;
    virtual ~Shape();
};

class Circle : public Shape {
  protected:
    double width;

  public:
    Circle(double w) { width = w; }
    virtual ~Circle() override;

    double Area() override { return 3.141592 * pow((width / 2), 2); }
};

Shape::~Shape() = default;

Circle::~Circle() = default;

void ShowArea(Shape &shape);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Circle circle(10);
    ShowArea(circle);

    return a.exec();
}

void ShowArea(Shape &shape) { cout << "Area : " << shape.Area() << endl; }
main.cpp (3)
// QObject
#include <QCoreApplication>
#include <QDebug>
#include <iostream>

using namespace std;

#pragma pack(1)
class Person : public QObject {
  public:
    Person(QObject *parent = nullptr);
    ~Person();

    void SetName(const QString &name);
    void SetAge(int age);
    void setSalary(double salary);

    QString GetName() const;
    int GetAge() const;
    double GetSalary() const;

    void Print() const;

  private:
    QString mName;
    int mAge;
    double mSalary;
};

Person::Person(QObject *parent) : QObject(parent) {}

Person::~Person() { qDebug() << mName << " : destructor"; }

void Person::SetName(const QString &name) { mName = name; }

void Person::SetAge(int age) { mAge = age; }

void Person::setSalary(double salary) { mSalary = salary; }

QString Person::GetName() const { return mName; }

int Person::GetAge() const { return mAge; }

double Person::GetSalary() const { return mSalary; }

void Person::Print() const { qDebug() << mName << " " << mAge << " " << mSalary; }

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Person *aaa = new Person;
    Person *bbb = new Person(aaa);
    Person *ccc = new Person(aaa);

    aaa->SetName("홍길동");
    aaa->SetAge(23);
    aaa->setSalary(5000.0);

    bbb->SetName("홍길서");
    bbb->SetAge(24);
    bbb->setSalary(6000.0);

    ccc->SetName("홍길남");
    ccc->SetAge(25);
    ccc->setSalary(7000.0);

    aaa->Print();
    bbb->Print();
    ccc->Print();

    delete aaa;
    return a.exec();
}
Reference

Prεv(Θld)   Nεxt(Nεw)
Content
Search     RSS Feed     BY-NC-ND