本人C++作业代码

一、加减乘除的计算器
头文件:a.h
#ifndef A_H//和头文件的名称要对应
#define A_H//和头文件的名称要对应
class jisuan
{
int x,y;
public:
jisuan(int,int);
void jia();
void jian();
void cheng();
void chu();
};
#endif
cpp:1
#include "a.h"
#include<iostream>
using namespace std;
jisuan::jisuan(int a,int b)
{
x=a;
y=b;
}
void jisuan::jia()
{cout<<"加的结果为:"<<x+y<<endl;
cout<<"减的结果为:"<<x-y<<endl;
cout<<"乘的结果为:"<<x*y<<endl;
cout<<"除的结果为:"<<x/y<<endl;}
cpp2:
#include "a.h"
int main()
{
jisuan j(5,10);
j.jia();
return 0;
}

二、输出一个字符和一个字符串
头文件:b.h
#ifndef B_H
#define B_H
class cuan
{
char *p;
public:
cuan(char);
cuan(char *);
void output();
~cuan();//析构函数
};
#endif

cpp:1
#include "b.h"
#include<iostream>
#include<cstring>
using namespace std;
cuan::cuan(char c)
{
p=new char;
*p=c;
}
cuan::cuan(char *y)
{
p=new char[strlen(y)+1];
strcpy(p,y);
}
void cuan::output()
{
while(*p!='\0')
{
cout<<*p;
p++;
}
cout<<endl;

}
cuan::~cuan()
{
delete [] p;
}


cpp:2
#include "b.h"
int main()
{
cuan a0('A');
cuan b0("Ajkhkj");
a0.output();
b0.output();
}
三、创建一个学生类。包含姓名,班级,年龄,学号
头文件:c.h
#ifndef C_H
#define C_H
class stud
{
char *xh;//long int xh;
char *name;//char *name;
unsigned int year;
char *banji;//char *banji;
public:
stud(char *,char *,unsigned int ,char * );
void out();
~stud();
};
#endif


cpp:1
#include"c.h"
#include<iostream>
#include<cstring>
using namespace std;
stud::stud(char *xh0,char *name0,unsigned int year0,char *banji0)
{
xh=new char[strlen(xh0)+1];
strcpy(xh,xh0);
name=new char[strlen(name0)+1];
strcpy(name,name0);
year = year0;
banji=new char[strlen(banji0)+1];
strcpy(banji,banji0);
}
void stud::out()
{
cout<<xh<<'\t'<<name<<'\t'<<year<<'\t'<<banji<<'\t'<<endl;
}
stud::~stud()
{
delete [] xh;
delete [] name;
delete [] banji;
}

cpp:2
#include"c.h"
int main()
{
stud s1("111307168","史国旭",20,"ruanjian-2");
s1.out();
}

你可能感兴趣的:(C++)