题目来源:http://acm.wust.edu.cn/problem.php?id=1406&soj=0
★最近开始学习C艹了,感觉自己智商低下,还是需要加深理解(于是写水了这篇博客)不过对于初学者,做这种题目找不到代码,也是很痛苦的 我就是这样awa
相关知识:
这里用到了一丢丢指针,还是char型指针,对于保存字符串还是挺方便 的(然而用string更方便,强烈安利)这里的*s 就可以看做是一个字符数组的首地址 s[0]就是字符串的第一个字符
然后还要理解 类外函数 与 类内函数 的区别,类内的函数使用前需要在前面注明对象名 如 s.hhh() 类外的函数不需要 hhh() 当然定义也是不同的,略咯~
思路:
跟着主函数和题目要求走,一个个定义就ok
注意:
可能自己运行时输入中文会乱码(如下所示),但是还是AC了,管它了~
代码:
#include<bits/stdc++.h>
using namespace std;
class Person
{
public:
Person(char *f, char *s, int m) {cout<<"Constructor id called!\n";forename=f; surname=s; money=m;}
Person(Person &p) {cout<<"Copy Constructor id called!\n"; forename=p.forename; surname=p.surname; money=p.money;}
~Person() {};
void display(){
cout<<"Person::display id called\n";
for(int i=0;forename[i]!='\0';i++) cout<<forename[i];
cout<<' ';
for(int i=0;surname[i]!='\0';i++) cout<<surname[i];
cout<<" has "<<money<<'.'<<endl;
}
private:
char *forename;
char *surname;
int money;
};
void fun(Person p)
{
cout<<"fun function id called!\n";
p.display();
}
int main()
{
char f[1000],s[1000];
double m;
int k=0;
while(cin>>f>>s>>m)
{
k++;
cout<<"Case #"<<k<<":"<<endl;
Person p(f,s,m);
fun(p);
}
return 0;
}