标题:初学c++,这个实在不知道要咋做,哪位大神帮个忙 题目在下面
只看楼主
遗情处有诗章
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2017-3-10
结帖率:75%
 问题点数:0 回复次数:3 
初学c++,这个实在不知道要咋做,哪位大神帮个忙 题目在下面
程序代码:
#include "stdafx.h"
#include<iostream>
using namespace std;

class homepage {
private:
    char *photo;
    int height;
    int width;
public:
    homepage(char*p, int h, int w)
    {}
    homepage()
    {}
    void set(char*p, int h, int w)
    {}
    homepage(homepage &hp)
    {}
    void print()const
    {
        cout << "photo:" << photo<< "," << "height:" << height << "," << "width:" <<width << endl;
    }
    ~homepage()
    {
        delete homepage;
    }

};
homepage::homepage(char*p, int h, int w)
{
    photo = p;
    height = h;
    width = w;

}

int main()
{
    return 0;
}

搜索更多相关主题的帖子: c++ include char int void 
2017-10-21 22:55
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
从你的上个帖子中的代码来看,你的老师教的是假C++(会点C,补点儿C++语法,就误以为自己会C++了)
所以我写个真正的C++代码,估计反而不能符合你老师的要求。

略尽人事吧
程序代码:
#include <iostream>
#include <cstring>

class homepage
{
public:
    homepage( const char* photo, int height, int width ) try : photo_(new char[strlen(photo)+1]), height_(height), width_(width)
    {
        strcpy( photo_, photo );
    }
    catch( ... )
    {
        throw;
    }

    homepage( const homepage& obj ) try : photo_(new char[strlen(obj.photo_)+1]), height_(obj.height_), width_(obj.width_)
    {
        strcpy( photo_, obj.photo_ );
    }
    catch( ... )
    {
        throw;
    }

    homepage( homepage&& obj ) noexcept : photo_(obj.photo_), height_(obj.height_), width_(obj.width_)
    {
        obj.photo_ = nullptr;
    }

    homepage& operator=( const homepage& obj )
    {
        if( this != &obj )
        {
            delete[] photo_;
            try {
                photo_ = new char[strlen(obj.photo_)+1];
            }
            catch( ... ) {
                throw;
            }

            strcpy( photo_, obj.photo_ );
            height_ = obj.height_;
            width_ = obj.width_;
        }
        return *this;
    }

    homepage& operator=( homepage&& obj ) noexcept
    {
        if( this != &obj )
        {
            delete[] photo_;
            photo_ = obj.photo_;
            height_ = obj.height_;
            width_ = obj.width_;

            obj.photo_ = nullptr;
        }
        return *this;
    }

    ~homepage()
    {
        delete[] photo_;
    }

private:
    char* photo_;
    int height_;
    int width_;

    friend std::ostream& operator<<( std::ostream& os, const homepage& obj )
    {
        return os << "photo = " << obj.photo_ << ", height = " << obj.height_ << ", width = " << obj.width_;
    }
};

using namespace std;

int main( void )
{
    homepage a = { "a", 1, 2 };
    cout << a << endl;

    homepage a2 = a;
    cout << a2 << endl;

    homepage a3 = std::move(a2);
    cout << a3 << endl;

    a = a3;
    cout << a << endl;

    a = std::move(a3);
    cout << a << endl;

    return 0;
}
2017-10-23 10:34
遗情处有诗章
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2017-3-10
得分:0 
回复 2楼 rjsp
哇,虽然看不太懂,但是多谢大神。我会努力学的。
虽然现在是初学,但是还是感觉好难啊
2017-10-29 14:13
遗情处有诗章
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2017-3-10
得分:0 
回复 2楼 rjsp
这里为什么要用到拷贝呢?
2017-10-29 14:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481639-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055823 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved