| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖
标题:C++类的继承问题和类的构造函数继承问题
只看楼主 加入收藏
nuclear_1
Rank: 1
来 自:成都
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-10-6
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:1 
C++类的继承问题和类的构造函数继承问题
楼主建了两个类,第二个类公有方式继承第一个类,这里类的构造函数继承有问题,还有字符串进行传递时应该怎么传递好呢?麻烦给位大侠看看楼主的代码问题出在哪里了!如果有改进后的程序,能解决字符串作为函数参数进行传递的问题就更好了!

#include<iostream.h>
class glass
{
private:
    int weight;
    int length;
    int width;
public:
    glass(int x,int y,int z)
        {
            weight=x;
            length=y;
            width=z;
        }
    display();
};
glass :: display()
{
    cout<<"The glass's weight:"<<weight<<",length:"<<length<<",width:"<<width<<endl;
}
class window : public glass
{
private:
    char color[10];
public:
    window (int x,int y,int z, char* c) : glass (int x,int y,int z)
        {
        color=c;
        }
    display();
};
window :: display()
{
    cout<<"The window's weight:"<<weight<<",length:"<<length<<",width:"<<width<<",color:"
    <<color<<endl;
}
int main()
{
    char color1[10]="yellow";
    char* color =color1;
glass number1(100,75,85);
window number2(99,74,84,color);
glass.display();
window.display();
return 0;
}














搜索更多相关主题的帖子: public include display private 
2013-06-05 21:40
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:10 
class 窗口 : public 玻璃              ?这样不太好吧

class glass
{
protected: // private: 要想派生类能够访问,就用protected吧
    int weight;
    int length;
    int width;
public:
    glass(int x,int y,int z)
    {
        weight=x;
        length=y;
        width=z;
    }
    void display();
};
void glass::display()
{
    cout<<"The glass's weight:"<<weight<<",length:"<<length<<",width:"<<width<<endl;
}
class window : public glass
{
private:
    char color[10];
public:
    window (int x,int y,int z, char* c) : glass (/*int */x,/*int */y,/*int */z) // 你对继承的使用还不了解,认真翻翻书吧
    {
        strcpy(color,c);// color=c; 看来你对字符串的操作一点都不熟悉
    }
    void display();
};
void window :: display()
{
    cout<<"The window's weight:"<<weight<<",length:"<<length<<",width:"<<width<<",color:"
        <<color<<endl;
}
int main()
{
    char color1[10]="yellow";
    char* color =color1;
    glass number1(100,75,85);
    window number2(99,74,84,color);
    number1.display();// glass.display(); 这个叫我怎么解析给你?
    number2.display(); // window.display();
    return 0;
}

注意红色的提示

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-06-06 12:59
快速回复:C++类的继承问题和类的构造函数继承问题
数据加载中...
 
   



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

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