注册 登录
编程论坛 VC++/MFC

C++类的继承问题和类的构造函数继承问题

nuclear_1 发布于 2013-06-05 21:40, 509 次点击
楼主建了两个类,第二个类公有方式继承第一个类,这里类的构造函数继承有问题,还有字符串进行传递时应该怎么传递好呢?麻烦给位大侠看看楼主的代码问题出在哪里了!如果有改进后的程序,能解决字符串作为函数参数进行传递的问题就更好了!

#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;
}














1 回复
#2
yuccn2013-06-06 12:59
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;
}

注意红色的提示
1