考试题/(ㄒoㄒ)/~~
来源:互联网 发布:java商城分销系统源码 编辑:程序博客网 时间:2024/06/05 09:13
我的考试题,我得反省一下我记几,啦啦啦...
1. 设计一个点类Point,其中包含点的坐标x和y 共2个数据成员,并设计1个友元函数,计算两点间的距离,并编写程序测试。
#include<iostream>
#include<cmath>
using namespace std;
class Distance;
class Point {
private:
float x, y;
public:
Point(float a, float b)
{
x = a; y = b;
}
float get_x()
{
return x;
}
float get_y()
{
return y;
}
friend float get_d(Point m,Point n);
};
float get_d(Point m, Point n)
{
float d;
d = sqrt((m.get_x() - n.get_x())*(m.get_x() - n.get_x()) + (m.get_y() - n.get_y())*(m.get_y() - n.get_y()));
cout << "两点之间的距离为:" << d<<endl;
return 0;
}
int main()
{
Point p1(5, 5);
Point p2(4, 4);
get_d(p1, p2);
return 0;
}
#include<cmath>
using namespace std;
class Distance;
class Point {
private:
float x, y;
public:
Point(float a, float b)
{
x = a; y = b;
}
float get_x()
{
return x;
}
float get_y()
{
return y;
}
friend float get_d(Point m,Point n);
};
float get_d(Point m, Point n)
{
float d;
d = sqrt((m.get_x() - n.get_x())*(m.get_x() - n.get_x()) + (m.get_y() - n.get_y())*(m.get_y() - n.get_y()));
cout << "两点之间的距离为:" << d<<endl;
return 0;
}
int main()
{
Point p1(5, 5);
Point p2(4, 4);
get_d(p1, p2);
return 0;
}
2. 利用静态数据成员的概念,编写一个类,可以统计目前存在多少个该类的对象,并在主程序中使用这个类。
#include<iostream>
using namespace std;
class student {
private:
char *name;
char *sex;
int age;
static int count;
public:
student(char *n, char *s, int a)
{
name = new char[strlen(n) + 1];
strcpy(name, n);
sex = new char[strlen(s) + 1];
strcpy(sex, s);
age = a;
++count;
}
~student()
{
delete name;
delete sex;
--count;
}
void print()
{
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "年龄:" << age << endl;
}
void print_count()
{
cout << "学生人数:" << count <<endl;
}
};
int student::count = 0;
void main()
{
student s1("张三", "男", 20);
s1.print();
s1.print_count();
student s2("小红", "女", 18);
s2.print();
s2.print_count();
}
using namespace std;
class student {
private:
char *name;
char *sex;
int age;
static int count;
public:
student(char *n, char *s, int a)
{
name = new char[strlen(n) + 1];
strcpy(name, n);
sex = new char[strlen(s) + 1];
strcpy(sex, s);
age = a;
++count;
}
~student()
{
delete name;
delete sex;
--count;
}
void print()
{
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "年龄:" << age << endl;
}
void print_count()
{
cout << "学生人数:" << count <<endl;
}
};
int student::count = 0;
void main()
{
student s1("张三", "男", 20);
s1.print();
s1.print_count();
student s2("小红", "女", 18);
s2.print();
s2.print_count();
}
3.设计一个字符串类String。并使其能完成以下功能:
(1)能使用“=”运算符完成两个字符串的赋值。
(2)能使语句 String p3(p1)正常运行。
(1)能使用“=”运算符完成两个字符串的赋值。
(2)能使语句 String p3(p1)正常运行。
(3)能使用“+”完成两个字符串的拼接。
#include<iostream>
#include<string>
using namespace std;
class String {
private:
char *s;
public:
String(const char* p = " ")
:s(new char[strlen(p) + 1])
{
strcpy(s, p);
}
String(const String &_s)
{
s = new char[strlen(_s.s) + 1];
strcpy(s, _s.s);
}
String &operator=(String &m)
{
if (this != &m)
{
if (s)
{
delete s;
}
s = new char[strlen(m.s) + 1];
strcpy(s, m.s);
}
return *this;
}
friend String operator+(String &m, String &n);
friend ostream &operator<<(ostream &output, String t);
};
String operator+(String &m,String &n)
{
return strcat(m.s, n.s);
}
ostream &operator<<(ostream &output, String t)
{
output << t.s;
return output;
}
void main()
{
String p1 = "hello!";
String p2 = p1;
String p3(p1);
cout << p1 << endl;
cout << p2 << endl;
cout << p3 << endl;
cout << p1 + p2 << endl;
}
#include<string>
using namespace std;
class String {
private:
char *s;
public:
String(const char* p = " ")
:s(new char[strlen(p) + 1])
{
strcpy(s, p);
}
String(const String &_s)
{
s = new char[strlen(_s.s) + 1];
strcpy(s, _s.s);
}
String &operator=(String &m)
{
if (this != &m)
{
if (s)
{
delete s;
}
s = new char[strlen(m.s) + 1];
strcpy(s, m.s);
}
return *this;
}
friend String operator+(String &m, String &n);
friend ostream &operator<<(ostream &output, String t);
};
String operator+(String &m,String &n)
{
return strcat(m.s, n.s);
}
ostream &operator<<(ostream &output, String t)
{
output << t.s;
return output;
}
void main()
{
String p1 = "hello!";
String p2 = p1;
String p3(p1);
cout << p1 << endl;
cout << p2 << endl;
cout << p3 << endl;
cout << p1 + p2 << endl;
}
阅读全文
0 0
- 考试题/(ㄒoㄒ)/~~
- 考试题
- 考试题
- 考试题
- 考试题
- C#考试题
- javaweb考试题
- c++考试题
- .net考试题
- .net考试题
- OOA考试题
- JAVA考试题
- 考试题记录
- C++考试题
- 考试题改错
- ado考试题
- java考试题
- 期末考试题
- go拾遗--使用竞争检测器标志来编译程序
- linux安装mysql后修改密码
- python学习(字符串转换)
- 24.职责链模式
- c++类模板的声明与定义
- 考试题/(ㄒoㄒ)/~~
- 25.中介模式
- 19. Remove Nth Node From End of List-python
- hodoop集群环境搭建----伪分布模式(Pseudodistributed mode)
- 计算机网络--概述
- cannot load oci dll,193/87-navicate连接oracle的解决方法
- 26.享元模式
- [LeetCode 解题报告]013. Roman to Integer
- 解决mybatis在xml映射文件里面写sql时候没有提示