第一题:
知识点:
- string类型的使用也是在namespace std中的,要使用std命名空间
head
#include <string>
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
class Bankaccount
{
std::string m_name;
std::string m_account;
double m_deposit;
public:
Bankaccount(std::string name, std::string account, double deposit)
{
m_name = name;
m_account = account;
m_deposit = deposit;
}
Bankaccount(){}
void Show();
void Indeposit(double money);
void Outdeposit(double money);
};
#endif // GOLF_H_INCLUDED
definition
#include <iostream>
#include "golf.h"
void Bankaccount::Show()
{
using namespace std;
cout << m_name << "\n";
cout << m_account << "\n";
cout << fixed << cout.precision(2)<< m_deposit << "\n";
}
void Bankaccount::Indeposit(double money)
{
m_deposit += money;
}
void Bankaccount::Outdeposit(double money)
{
m_deposit -= money;
}
main
#include <iostream>
#include "golf.h"
int main()
{
Bankaccount User1("Tony Stark", "3050006", 36000.50);
User1.Indeposit(500);
User1.Outdeposit(200);
User1.Show();
return 0;
}
第二题:
知识点:
- 没什么东西
head
#include <string>
#include <cstring>
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
class Person
{
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person() {lname = ""; fname[0] = '\0';}
Person(const std::string & ln, const char * fn = "Heyyou")
{
lname = ln;
strcpy(fname, fn);
}
void Show() const;
void FormalShow() const;
};
#endif // GOLF_H_INCLUDED
difination
#include <iostream>
#include "golf.h"
void Person::Show()const
{
using namespace std;
cout << lname << " ";
cout << fname << "\n";
}
void Person::FormalShow()const
{
std::cout << fname << " ";
std::cout << lname << "\n";
}
main
#include <iostream>
#include "golf.h"
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
one.FormalShow();
std::cout << std::endl;
two.Show();
two.FormalShow();
std::cout << std::endl;
three.Show();
three.FormalShow();
return 0;
}
第三题:
知识点
- 就是把结构体换成类,没什么意思
改的地方就是类定义和构造函数,具体如下
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
const int Len = 40;
class golf
{
char fullname[Len];
int handicap;
public:
golf(const char * name, int hc);
golf();
void Handicap(int hc);
void showgolf()const;
};
#endif // GOLF_H_INCLUDED
// contain function called in file1
#include <iostream>
#include "golf.h"
#include <cstring>
golf::golf(const char * name, int hc)
{
strcpy(fullname, name);
handicap = hc;
}
golf::golf()
{
using namespace std;
cout << "Enter the name and the value:"<< endl;
cin.getline(fullname,Len);
cin >> handicap;
cin.get();
}
void golf::showgolf()const
{
std::cout << fullname << std::endl;
std::cout << handicap << std::endl;
}
void golf::Handicap(int hc)
{
handicap = hc;
}
第四题:
知识点
- 和上一题太像,不做了,就是放在命名空间里定义而已
第七题:
知识点
- 其实也很简单
head
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
#include <cstring>
class Plorg
{
char m_name[19];
int m_CI;
public:
Plorg(const char *name , int CI = 50);
Plorg() {strcpy(m_name, "Plorga"); m_CI = 50;}
void setCI(int CI);
void showCI();
};
#endif // GOLF_H_INCLUDED
definition
// contain function called in file1
#include <iostream>
#include <cstring>
#include "golf.h"
Plorg::Plorg(const char *name , int CI)
{
strcpy(m_name, name);
m_CI = CI;
}
void Plorg::setCI(int CI)
{
m_CI = CI;
}
void Plorg::showCI()
{
std::cout << m_name << " CI = " << m_CI << "\n";
}
main
#include <iostream>
#include "golf.h"
int main()
{
Plorg test1;
Plorg test2("Romance", 60);
test1.showCI();
test1.setCI(40);
test1.showCI();
test2.showCI();
return 0;
}
暂时抽象数据类型的不太懂,过后再回来做