2012-06-22 10:49:49Morris

[C++][OOP] Lab4 仿作練習

代碼很粗糙, 不怎麼會用 new 函數, this 也不怎麼好用了,
this 難道不能呼叫 construct method ?
super 難道也不能呼叫 construct method ?
Java 所提供的 string operator+ 實在太方便了, 整數可以直接變成 string,
而在 C++ 卻要多好幾不做轉換



#include <iostream>

#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Person {
    public:
        Person() {
            _name = "No name";
        }
        Person(string _name) {
            this->_name = _name;
        }
        Person(Person *obj) {
            _name = obj->_name;
        }
        void setName(string _name) {
            this->_name = _name;
        }
        string getName() {
            return _name;
        }
        string toString() {
            return _name;
        }
        bool operator==(Person obj) {
            return _name == obj._name;
        }
    protected:
    private:
        string _name;
};
class Vehicle {
    public:
        Vehicle(string manufacturer, int cylinder, Person owner) {
            _manufacturer = manufacturer;
            _cylinder = cylinder;
            _owner = new Person(owner);
        }
        Vehicle() {
            _manufacturer = "None";
            _cylinder = 0;
            _owner = new Person();
        }
        string getManufacturer() {
            return _manufacturer;
        }
        int getCylinder() {
            return _cylinder;
        }
        Person* getOwner() {
            return _owner;
        }
        void setManufacturer(string p) {
            _manufacturer = p;
        }
        void setCylinder(int cylinder) {
            _cylinder = cylinder;
        }
        void setOwner(Person *owner) {
            _owner = new Person(owner);
        }
        bool operator==(Vehicle obj) {
            return _manufacturer == obj._manufacturer &&
                _cylinder == obj._cylinder &&
                _owner == obj._owner;
        }
        string toString() {
            char str[35];
            itoa(_cylinder, str, 10);
            return _manufacturer + ", "
            + str + " cylinders, owned by " + _owner->toString();
        }
    private:
        string _manufacturer;
        int _cylinder;
        Person *_owner;

};
class Truck : public Vehicle {
    public:
        Truck() {
            _loadCap = 0;
            _towingCap = 0;
        }
        Truck(string manufacturer, int cylinder, Person *owner,
              double loadCap, int towingCap) {
            setManufacturer(manufacturer);
            setCylinder(cylinder);
            setOwner(owner);
            _loadCap = loadCap;
            _towingCap = towingCap;
        }
        double getLoadCap() {
            return _loadCap;
        }
        int getTowingCap() {
            return _towingCap;
        }
        void setLoadCap(double loadCap) {
            _loadCap = loadCap;
        }
        void setTowingCap(int towingCap) {
            _towingCap = towingCap;
        }
        string toString() {
            char str1[35], str2[35];
            sprintf(str1, "%g", _loadCap);
            sprintf(str2, "%d", _towingCap);
            string s1(str1), s2(str2);
            return Vehicle::toString() + ", loadCap " + s1 + ", towingCap " + s2;
        }
        bool operator==(Truck obj) {
            return toString() == obj.toString();
        }
    private:
        double _loadCap;
        int _towingCap;
};
int main()
{
    Person p1;
    cout << p1.toString() << endl;
    p1.setName("Morris");
    cout << p1.getName() << endl;

    Person p2("Ping-Lin");
    cout << p2.toString() << endl;

    Person p3(p1);
    cout << p3.toString() << endl;
    cout << (p1 == p3) << " " << (p2 == p3) << endl;

    Vehicle v1;
    cout << v1.toString() << endl;
    v1.setManufacturer("Porsche");
    v1.setCylinder(4);
    v1.setOwner(new Person("Dijsktra"));
    cout << v1.getManufacturer() << endl;
    cout << v1.getCylinder() << endl;
    cout << v1.getOwner()->toString() << endl;

    Vehicle v2("Porsche", 8, new Person("Ping"));
    cout << v2.toString() << endl;

    Vehicle v3(v1);
    cout << (v1 == v3) << " " << (v2 == v3) << endl;

    Truck t1;
    cout << t1.toString() << endl;
    t1.setLoadCap(10.20);
    t1.setTowingCap(5);
    cout << t1.getLoadCap() << endl;
    cout << t1.getTowingCap() << endl;

    Truck t2("NISSAN", 6, new Person("Ping"), 10.95, 20);
    cout << t2.toString() << endl;

    Truck t3(t1);
    cout << t3.toString() << endl;
    cout << (t1 == t3) << " " << (t2 == t3) << endl;
    return 0;
}

和風信使 2012-06-22 12:19:20

#include<sstream>
template<class T> string any2s(T a)
{
ostringstream sout;
sout<<a;
return sout.str();
}

版主回應
上網搜索過這種了 2012-06-22 12:34:28