| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1014 人关注过本帖
标题:析构函数的例子
只看楼主 加入收藏
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:12 
析构函数的例子
#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;

课本一道例题,运行不出来,
搜索更多相关主题的帖子: private display include public called 
2013-10-15 20:08
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
什么运行不出来?
至少把问题描述清除

ps  你的main函数漏了右括号

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-10-15 22:09
a462410594
Rank: 2
等 级:论坛游民
帖 子:75
专家分:64
注 册:2011-11-17
收藏
得分:0 
什么课本呀?
2013-10-15 22:53
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 2楼 yuccn
我加了右括号,程序也没有运行出来
2013-10-16 12:43
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 3楼 a462410594
谭浩强版的c++课本的一道例题,程序运行不出来啊
2013-10-16 12:44
在这里爬起
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:43
专家分:113
注 册:2013-8-9
收藏
得分:5 
#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 编辑 ]
2013-10-16 13:48
小核桃pp
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2013-7-5
收藏
得分:5 
#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;

} 出了,你试试再
2013-10-16 15:28
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
test

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-10-16 16:02
staticor
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-7-16
收藏
得分:0 
程序代码:
#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;
}
2013-10-16 16:40
staticor
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-7-16
收藏
得分:0 
程序代码:
#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;
}
2013-10-16 16:41
快速回复:析构函数的例子
数据加载中...
 
   



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

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