1.结果:
constructor called
copy constructor called
constructor called
constructor called
constructor called
constructor called
copy constructor called
copy constructor called
constructor called
copy constructor called
#include <iostream>
using namespace std;
class Fraction {
//数据成员,访问控制属性默认是私有
private:
int m_numerator = 0; // 分子默认为0; C++11
int m_denominator = 1; //分母默认为1;
public://公有成员函数
int getnumerator() const{ return m_numerator; }
int getdenominator() const { return m_denominator; }
Fraction(int above = 0, int below = 1) :
m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator),
m_denominator(rhs.m_denominator) {
cout << "Copy constructor called" << endl;
}
};
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(),
divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(),
divident.getdenominator() * divisor.getnumerator());
return result;
}
class Fraction {
public:
~Fraction()
{
}
};
ostream& print(ostream& os, const &a) {
ostream << a.getnumerator() <<" "<< a.getdenominator();
return os;
}
void Fraction::reduce() {
int n = gcd(m_numerator, m_denominator);
m_denominator /= n;
m_numerator /= n;
}
void makecommon(Fraction& a, Fraction& b) {
a.m_numerator *= b.m_denominator;
b.m_numerator *= a.m_denominator;
b.m_denominator = a.m_denominator *= b.m_denominator;
}
Fraction operator/(const Fraction& left, const Fraction& right) {
Fraction result(left.numerator() * right.denominator(), left.denominator() * right.numerator());
return result;
}
int main() {
Fraction a;
Fraction b(a);
Fraction c = Fraction(3, 2);
Fraction d1(2, 3), d2(4, 5);
Fraction e1 = divide1(d1, d2);
Fraction e2 = divide2(d1, d2);
}
#include <iostream>
#include <vector>
using namespace std;
int search(int a[], int n) {
for (int i = 0; i < n; i++) {
if (a[i] == 17) {
return i + 1;
}
}
}
int msearch(int a[], int high, int low) {
while (low <= high) {
int pos = (low + high) / 2;
if (17 < a[pos]) {
high = pos - 1;
}
else if (17 > a[pos]) {
low = pos + 1;
}
else if (17 == a[pos]) {
return pos + 1;
}
}
}
void prime(int a[], int n) {
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
}
else if (a[i] == 2 || a[i] == 3) {
}
else {
for (int n = 2; n < a[i]; n++) {
if (a[i] % n == 0) {
}
else {
vector <int> vi;
vi.push_back(a[i]);
}
}
}
}
}
void sort(vector <int> vi, int n) {
for (int i = 1; i <= n - 1; i++) {
for (int j = 0; j < n - i; j++) {
if (vi.at(j + 1) < vi.at(j)) {
int t = vi.at(j + 1);
vi.at(j + 1) = vi.at(j);
vi.at(j) = t;
}
}
}
}
int main() {
cout << "a中17所在下标" << endl;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
cout << search(a, 5) << endl;
cout << "b中17所在下标" << endl;
cout << msearch(b, 59, 2) << endl;
vector <int> vi;
prime(a, 5);
prime(b, 5);
void sort(vector <int> vi, int n = 7);
for (int i = 0; i < 7; i++) {
cout << vi.at(i) << endl;
}
return 0;
}
3.结果:
constructor of point
copy constructor of point
constructor of circle
destructor of point
end
destructor of circle
destructor of point
#include <iostream>
#include <math.h>
using namespace std;
class Point {
double m_x = 0, m_y = 0;
public:
double x() { return m_x; }
double y() { return m_y; }
friend double D(Point, Point);
Point(double x = 0, double y = 0) : m_x(x), m_y(y) {
cout << "Constructor of Point" << endl;
}
Point(const Point& p) :m_x(p.m_x), m_y(p.m_y) {
cout << "Copy constructor of Point" << endl;
}
~Point() {
cout << "Destructor of Point" << endl;
}
};
class Circle {
Point m_center; double m_radius = 1.0;
public:
double S() {
double pi = 3.14;
double s = pi * m_radius * m_radius;
return s;
}
double L() {
double pi = 3.14;
double l = 2 * pi * m_radius;
return l;
}
Circle(double r = 1, const Point& p = Point()) :m_center(p), m_radius(r) {
cout << "Constructor of Circle" << endl;
}
~Circle() {
cout << "Destructor of Circle" << endl;
}
};
int pow(int a,int b) {}
int sqrt(int a) {}
double D(Point a, Point b) {
double m = sqrt(a.m_x - b.m_x) + sqrt(a.m_y - b.m_y);
}
int main()
{
Circle a(2, Point(1, 1));
cout << "end" << endl;
Point c(3,2);
cout << c.x() << c.y() << endl;
Circle b(4, Point(2, 1));
double m = D(Point(1, 1), Point(2, 1));
cout << pow(m,2) << endl;
return 0;
cout << a.S() << endl;
cout << a.L();
}
题目 1、类的定义与基本操作
class Fraction {
//数据成员,访问控制属性默认是私有
int m_numerator = 0; // 分子默认为0; C++11
int m_denominator = 1; //分母默认为1;
public://公有成员函数
Fraction(int above = 0, int below = 1) :
m_numerator(above), m_denominator(below) {
cout << “Constructor called” << endl;
}
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator),
m_denominator(rhs.m_denominator) {
cout << “Copy constructor called” << endl;
}
};
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(),
divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(),
divident.getdenominator() * divisor.getnumerator());
return result;
}
说明执行下列语句后,分别执行的什么操作,会输出什么?
Fraction a;
Fraction b(a);
Fraction c = Fraction(3, 2);
Fraction d1(2, 3), d2(4, 5);
Fraction e1 = divide1(d1, d2);
Fraction e2 = divide2(d1, d2);
在上述类的定义基础上,完善下列操作: 1) 显示定义析构函数; 2) 获取分数的分子;
3) 获取分数的分母; 4) 实现分数的约分; 5) 实现对两个分数对象进行通分; 6) 使用 operator/操作符重载实现两个分数的除法运算。
题目 2、数组与函数的综合应用
已知:int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
编写程序查找数组中是否存在某个指定元素;将数组a和数组b中的素数不重不漏地合并到
一个vector容器c中,然后按照下标访问的方式手动对容器c中的数据,按从小到大顺序重新
排序。要求依次实现: 1) 编写顺序查找法函数和折半查找法函数,分别在数组a和数组b中查找元素17所在的下标
并输出。 2) 编写判断素数函数和排序函数,并对容器c中的结果进行输出。
题目 3、类的定义与基本操作
class Point {
double m_x = 0, m_y = 0;
public:
Point(double x=0, double y=0) : m_x(x), m_y(y) {
cout << “Constructor of Point” << endl;
}
Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
cout << “Copy constructor of Point” << endl;
}
~Point() {
cout << “Destructor of Point” << endl;
}
};
class Circle {
Point m_center; double m_radius = 1.0;
public:
Circle(double r=1, const Point &p=Point()) :m_center§, m_radius® {
cout << “Constructor of Circle” << endl;
}
~Circle() {
cout << “Destructor of Circle” << endl;
}
};
int main()
{
Circle a(2, Point(1, 1));
cout << “end” << endl;
return 0;
}1) 说明上述程序执行流程和输出结果; 2) 在Point类中完善获取点的横坐标、获取点的纵坐标成员函数,并在主函数中测试; 3) 通过友元函数实现平面上任意两个点的距离计算辅助函数; 4) 在Circle类中完善圆的面积计算与圆的周长计算成员函数,并在主函数中测试;