| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1141 人关注过本帖
标题:初学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
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册: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
快速回复:初学c++,这个实在不知道要咋做,哪位大神帮个忙 题目在下面
数据加载中...
 
   



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

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