| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 720 人关注过本帖
标题:高手来看下
只看楼主 加入收藏
广夜
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-1-5
收藏
 问题点数:0 回复次数:5 
高手来看下
#include"name.h"

#include<string>

using std::string;

#include<iostream>

using std::ostream;
using std::istream;

#include<iomanip>

using std::setw;
using std::left;

#include<cstring>

using std::strlen;
using std::strcpy;

int Name::number = 0;

Name::Name(const char * const f , const char * const l)
{
    setFirstName(f);
    setLastName(l);
    Name::number ++;
}
Name::~Name()
{
    delete [] firstname;
    delete [] lastname;
    number--;
}

void Name::setFirstName( const char * const first)
{
    firstname = new char[ strlen( first ) + 1 ];
    strcpy( firstname , first );
}

void Name::setLastName( const char * const last)
{
    lastname = new char[ strlen( last ) + 1 ];
    strcpy( lastname , last );
}

char *Name::getFirstName()
{
    return firstname;
}

char *Name::getLastName()
{
    return lastname;
}

ostream &operator<<( ostream &output , const Name &m )
{
    output <<"Welcome to GradeBood System" <<'\n'<<"Number is: "<<setw(2)<<left<< m.number << '\t'<<"Name is: " << m.firstname << " " << m.lastname;
    return output;
}

istream &operator>>( istream &input , Name &n )
{
    input >> setw(25) >>  n.getFirstName() >> n.getLastName();
    return input;
}


_____________________________________________________________________________________________________________________________________________

//name.h
//name class definition

#ifndef NAME_H
#define NAME_H

#include<iostream>
using std::ostream;
using std::istream;

#include<string>
using std::string;

class Name
{
    friend ostream &operator<<( ostream & , const Name & );
    friend istream &operator>>( istream & , Name & );

public:
    Name(const char * const = "",const char * const = "");
    ~Name();
    void setFirstName(const char * const);
    char *getFirstName();
    void setLastName( const char * const);
    char *getLastName();
    int getNumber(){return number;}

private:
    char *firstname;
    char *lastname;
    static int number;
};
#endif
_____________________________________________________________________________________________________________________________________________

#include "name.h"

#include <iostream>
using std::cout;
using std::cin;

#include <string>
using std::string;


int main()
{
    char *first = "";
    char *last = "";

    cout<< "Please enter the frist name or enter \"end\" to exit"<<'\n';
    cin >> first;
    cout << "Please enter the last name or enter \"end\" to exit"<<'\n';
    cin >> last;

    Name student(first , last);
    cout << student;
    cout<<'\n'<<"please inter the first name and the last of a new student:"<<'\n';
    cin >> student;
    cout << student;

    return 0;
}

1>------ 已启动生成: 项目: GradeBook, 配置: Debug Win32 ------
1>正在编译...
1>name.cpp
1>.\name.cpp(40) : warning C4996: “strcpy”被声明为否决的
1>        E:\公用软件\学习软件\Microsoft Visual Studio 8\VC\include\string.h(73) : 参见“strcpy”的声明
1>        消息:“This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”
1>.\name.cpp(46) : warning C4996: “strcpy”被声明为否决的
1>        E:\公用软件\学习软件\Microsoft Visual Studio 8\VC\include\string.h(73) : 参见“strcpy”的声明
1>        消息:“This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”
1>正在链接...
1>正在嵌入清单...
1>生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\GradeBook\GradeBook\Debug\BuildLog.htm”
1>GradeBook - 0 个错误,2 个警告

运行的时候,一输入就出错。 。高手帮帮忙。。怎么回事,该怎么改,高手帮帮忙,感激不尽!!!!!
2008-01-05 13:48
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
你的构造函数是有问题的,,,对象还没有完成初始化就调用了成员函数!!

樱花大战,  有爱.
2008-01-05 15:08
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我都没办法运行.顶一下,让高手给你解答,让我也学学啊....
2008-01-06 22:17
sharep
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-1-5
收藏
得分:0 
char *first = "";
    char *last = "";

    cout<< "Please enter the frist name or enter \"end\" to exit"<<'\n';
    cin >> first;


指针能用吗?
cin>>*first;
2008-01-06 23:06
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
呵呵,都没分配内存~~~

樱花大战,  有爱.
2008-01-07 15:39
快速回复:高手来看下
数据加载中...
 
   



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

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