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

析构函数的例子

呜呜1 发布于 2013-10-15 20:08, 1014 次点击
#include<iostream>
#include <string>
using  namespace  std;
class  Student
{public:
student(int n,string nam,char s)
{num=n;
name=nam;
sex=s;
cout<<"Constructor  called."<<endl;}

~Student ()
{cout<<"Destructor  called."<<endl;}
void  display()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl<<endl;}
private:
    int  num;
    string  name:
    char   sex;
};
int  main()
{Student  stud1(101,"Wangli",'f');
stud1.display();
Student  stud2(102,"Limin",'m');
stud2.display();
return  0;

课本一道例题,运行不出来,
12 回复
#2
yuccn2013-10-15 22:09
什么运行不出来?
至少把问题描述清除

ps  你的main函数漏了右括号
#3
a4624105942013-10-15 22:53
什么课本呀?
#4
呜呜12013-10-16 12:43
回复 2楼 yuccn
我加了右括号,程序也没有运行出来
#5
呜呜12013-10-16 12:44
回复 3楼 a462410594
谭浩强版的c++课本的一道例题,程序运行不出来啊
#6
在这里爬起2013-10-16 13:48
#include <string>
#include<iostream>
using  namespace  std;
class  Student
{
public:
       Student
(int n,string nam,char s)
       {
           num=n;
           name=nam;
           sex=s;
       cout<<"Constructor  called."<<endl;

       }
LZ你应该是把Student写错了,应该是大写Student(int n,string nam,char s)


[ 本帖最后由 在这里爬起 于 2013-10-16 13:49 编辑 ]
#7
小核桃pp2013-10-16 15:28
#include<iostream>
#include <string>
using  namespace  std;
class  Student
{public:
Student(int n,string nam,char s)
{num=n;
name=nam;
sex=s;
cout<<"Constructor  called."<<endl;}

~Student ()
{cout<<"Destructor  called."<<endl;}
void  display()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl<<endl;}
private:
    int  num;
    string  name;
    char   sex;
};
int  main()
{Student  stud1(101,"Wangli",'f');
stud1.display();
Student  stud2(102,"Limin",'m');
stud2.display();
return  0;

} 出了,你试试再
#8
yuccn2013-10-16 16:02
test
#9
staticor2013-10-16 16:40
程序代码:
#include <iostream>
#include <string>

using namespace std;

class Student
{
private:
    int num;
    string name;
    char sex;

public:
    Student (int n, string nam, char s)
    {
        num = n;
        name = nam;
        sex = s;
        cout << "Constructor  called." << endl;
    }

    ~Student ()
    {
        cout << "Destructor  called." << endl;
    }

    void display ()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl << endl;
    }
};

int main()
{
    Student stud1 (101, "Wangli", 'f');
    stud1.display ();
    Student stud2 (102, "Limin", 'm');
    stud2.display ();
    return  0;
}
#10
staticor2013-10-16 16:41
程序代码:
#include <iostream>
#include <string>

using namespace std;

class Student
{
private:
    int num;
    string name;
    char sex;

public:
    Student (int n, string nam, char s)
    {
        num = n;
        name = nam;
        sex = s;
        cout << "Constructor  called." << endl;
    }

    ~Student ()
    {
        cout << "Destructor  called." << endl;
    }

    void display ()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl << endl;
    }
};

int main()
{
    Student stud1 (101, "Wangli", 'f');
    stud1.display ();
    Student stud2 (102, "Limin", 'm');
    stud2.display ();
    return  0;
}
#11
yuccn2013-10-16 17:29
test
#12
苑天尤2013-10-17 19:19
#include<iostream>
#include<string>
using std::string;
using std::cout;
using std::endl;
class Student
{

private:
int m_num;
string m_name;
char m_sex;
public:
Student(int num,string name,char sex):m_num(num),m_name(name),m_sex(sex)
{
cout << "Constructor  called." << endl;
     }
~student()
{
cout << "Destructor  called." << endl;
     }
 void display ()
     {
         cout << "num:" << num << endl;
         cout << "name:" << name << endl;
         cout << "sex:" << sex << endl << endl;
}
};
int main()
 {
     Student stud1 (101, "Wangli", 'f');
     stud1.display ();
     Student stud2 (102, "Limin", 'm');
     stud2.display ();
     return  0;
 }

 
#13
序曲萧邦2013-10-18 17:29
细节问题。看看编程软件下面的提示。双击可以看看那里出问题。函数后面不用分号void  display()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl<<endl;}
private:
    int  num;
    string  name:
    char   sex;
}
还有int main()
{} 括号括回去
1