Problem D: 字符类的封装

发布时间:2023-05-26 09:40:50

Problem D: 包装字符类

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 1078

Solved: 808

[Submit][Status][Web Board]

Description

让我们先做一个简单的练习,练习你的手!现在你需要写一个character类,包装char的基本数据类型。该类别需要以下成员函数:

1. 无参结构函数。

2. Character构建函数(char):初始化数据成员使用参数。

3. void setCharacter(char):重新设置字符值。

4. int getAsciiCode():ASII码返回字符。

5. char getCharacter():返回字符值。

6. 析构函数。

Input

只有一行输入,包含一个合法的、可打印的字符。

Output

输出有很多行,请参考样本编写相应的函数。

Sample Input

c

Sample Output

Default constructor is called!Character a is created!ch1 is c and its ASCII code is 99.ch2 is a and its ASCII code is 97.Character a is erased!Character c is erased!

HINTAppend Code

append.cc,

[ Submit][Status][Web Board]

한국어<

中文

فارسی

English

ไทย

All Copyright Reserved 2010-2011

SDUSTOJ TEAM

GPL2.0 2003-2011

HUSTOJ Project TEAM

Anything about the Problems, Please Contact Admin:

admin

#include <iostream>using namespace std;class Character{private:    char c;public:    Character()    {        cout<<"Default constructor is called!"<<endl;    }    Character(char s)    {        cout<<"Character "<<s<<" is created!"<<endl;        c = s;    }    void setCharacter(char x)    {        c = x;    }    int getAsciiCode(){return c;}    char getCharacter(){return c;}    ~Character()    {        cout<<"Character "<<c<<" is erased!"<<endl;    }};int main(){    char ch;    Character ch1, ch2('a');    cin>>ch;    ch1.setCharacter(ch);    cout<<"ch1 is "<<ch1.getCharacter()<<" and its ASCII code is "<<ch1.getAsciiCode()<<"."<<endl;    cout<<"ch2 is "<<ch2.getCharacter()<<" and its ASCII code is "<<ch2.getAsciiCode()<<"."<<endl;    return 0;}

上一篇 单链表
下一篇 动力节点Docker深入浅出教程—Docker镜像

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题