注册 登录
编程论坛 C++教室

请前辈比较两个程序,一个有问题一个没问题,为什么?

heboqian 发布于 2009-08-13 12:05, 714 次点击
下面两个程序做比较,为什么第一个程序没问题,第二个程序有问题?
第一个程序:

#include <iostream>
#include <string.h>

class Student
{
public:
    Student::Student(char *n = "zhangsan", int a = 23, int i = 1009, int g = 3, int r = 90)
    {
        strcpy(name,n);
        age = a;
        id = i;
        grade = g;
        result = r;
        std::cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << std::endl;
    }
protected:
    char name[30];
    int age;
    int id;
    int grade;
    int result;
};

int main()
{
    Student st("lisi");
    return 0;
}

第二个程序:

#include <iostream>
#include <string.h>

class Student
{
public:
    Student::Student(char *n, int a, int i, int g, int r);
protected:
    char name[30];
    int age;
    int id;
    int grade;
    int result;
};

int main()
{
    Student st("lisi");
    return 0;
}

Student::Student(char *n = "zhangsan", int a = 23, int i = 1009, int g = 3, int r = 90)
{
    strcpy(name,n);
    age = a;
    id = i;
    grade = g;
    result = r;
    std::cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << std::endl;
}
9 回复
#2
xufen3402009-08-13 12:20
构造函数参数默认值的概念。
第一个:
由于参数有默认值,调用构造函数可以省参数。
第二个:
参数没有默认值,没有调用构造函数,编译器错误调用拷贝构造函数。参数出错.
#3
heboqian2009-08-18 10:20
回复 2楼 xufen340

谢谢前辈,我再好好看看这块的知识吧
#4
战龙2009-08-21 16:11
程序代码:
#include <iostream>
#include <string.h>

class Student
{
public:
    Student(char *n, int a, int i, int g, int r);
protected:
    char name[30];
    int age;
    int id;
    int grade;
    int result;
};

Student::Student(char *n = "zhangsan", int a = 23, int i = 1009, int g = 3, int r = 90)
{
    strcpy(name,n);
    age = a;
    id = i;
    grade = g;
    result = r;
    std::cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << std::endl;
}

int main()
{
    Student st("lisi");
    return 0;
}
这样就没问题了
#5
serious2009-08-23 01:08
纯C++的程序
&lt;string.h> 是 "deprecated", 你应该用 &lt;cstring>
但是 "std::string" 比 "char*" 容易的
而且你可以用 "initialization list"

这个程序是纯C++ :
程序代码:
#include <iostream>
#include <string>

using namespace std;

class Student
{
    public:
            Student(string n, int a, int i, int g, int r);
    protected:
            string name;
            int age;
            int id;
            int grade;
            int result;
};

Student::Student(string n = "zhangsan", int a = 23, int i = 1009, int g = 3, int r = 90)
    : name(n), age(a), id(i), grade(g), result(r) // "Initialization list"
{
    cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << endl;
}


int main()
{
    Student st("lisi");

    return 0;
}


[ 本帖最后由 serious 于 2009-8-23 01:09 编辑 ]
#6
newCpp2009-08-23 08:18
晕死了。看不懂
#7
阿闲2009-08-23 15:55
马后炮:
    如果在#include<iostream>,后面加上using namepace std;
那后面的std::cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << std::endl;可以写成
cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << endl;
这可是个好习惯啊。
#8
bubuhui2009-08-25 00:11
楼主   第二个程序里面   Student::Student(char *n, int a, int i, int g, int r);后面的分号错了 应该是;
构造函数放main函数之前就可以了 是一样的
#9
serious2009-08-25 04:50
马后炮:
    如果在#include<iostream>,后面加上using namepace std;
那后面的std::cout &lt;&lt; name << "  " << age << "  " << id << "  " << grade << "  " << result << std::endl;可以写成
cout << name << "  " << age << "  " << id << "  " << grade << "  " << result << endl;
这可是个好习惯啊。


但是不必须用"using"头文件里面

比如 :
test.h :
using namespace std;


test.cpp :
程序代码:
#include "test.h"

#include <vector>

class vector
{
};

int main(void)
{
    vector v1;// 汇编错误
    ::vector v2; // 好
}


[ 本帖最后由 serious 于 2009-8-25 04:53 编辑 ]
#10
lnwxlwc2009-08-25 11:50
主要问题在这里
首先 通过运行 你那个分号 的输入是中文状态输入的。。还有 就是  类的成员函数 定义在main函数之前了。。请自行更改后 再试要再不对  就怪了
1