//初始化
Person(int a,int b,int c):A(a),B(b),C(c)
{}
//调用
Person p(1,2,3)
注意冒号在哪
#include<iostream>
#include<string>
using namespace std;
//类对象作为类成员
//手机类
class Phone
{
public:
string m_PName;
Phone(string pName)
{
m_PName = pName;
}
//手机品牌
};
//人类
class Person
{
public:
//Phone m_Phone=pName 隐式转换法
Person(string name, string pName):m_Name(name),m_Phone(pName)
{}
string m_Name;
Phone m_Phone;
};
void test1()
{
Person p("张三", "苹果");
cout << p.m_Name << "拿着:" << p.m_Phone << endl;
}
书上习题P242
#include<iostream>
using namespace std;
//T1
class Book
{
private:
string bookname;
int price;
int number;
public:
void setBook(string, int, int);
void display();
void borrow();
void restore();
};
void Book::setBook(string name, int pri, int num)
{
bookname = name;
price = pri;
number = num;
}
void Book::display()
{
cout << "存书情况:" << endl << "书名:" << bookname << endl
<< "价格:" << price << endl << "目前存书量:" << number << endl;
}
void Book::borrow()
{
if (number <= 0)
{
cout << "抱歉,存书量为零。" << endl;
}
else number = number - 1;
cout << "此时存书量为:" << number << endl;
}
void Book::restore()
{
number = number + 1;
cout << "此时存书量为:" << number << endl;
}
//T2
class Box
{
private:
int length;
int width;
int height;
public:
void setBox(int, int, int);
void volume();
void input();
};
void Box::setBox(int len, int wid, int hei)
{
length = len;
width = wid;
height = hei;
}
void Box::input()
{
cout << "请输入盒子的长(米):";
cin >> length;
cout << "请输入盒子的宽(米):";
cin >> width;
cout << "请输入盒子的高(米):";
cin >> height;
}
void Box::volume()
{
int vol;
vol = length * width * height;
cout << "盒子体积为:" << vol << "立方米" << endl;
}
//T3
class Student
{
private:
double score;
static double total;
static double count;
public:
void scoretotalcount(double s)
{
score = s;
total = total + score;
count++;
}
static double sum()
{
return total;
}
static double average()
{
return total / count;
}
};
double Student::total = 0;
double Student::count = 0;
//T4
struct point
{
double x; double y;
};
class line
{
private:
double a, b;
public:
line(double u, double v)
{
a = u; b = v;
}
void print()
{
cout << "y = " << a << "x + " << b << endl;
}
friend point setPoint(line& l1, line& l2);
};
point setPoint(line& l1, line& l2)
{
point p;
p.x = (l2.b - l1.b) / (l1.a - l2.a);
p.y = (p.x) * l1.a + l1.b;
return p;
}
int main()
{
//T1
Book xyj;
xyj.setBook("西游记", 50, 100);
int i;
xyj.display();
cout << "借书请输入1,还书请输入0:" << endl;
cin >> i;
if (i == 1)
{
xyj.borrow();
}
if (i == 0)
{
xyj.restore();
}
cout << "----------------------------" << endl;
//T2
Box b;
b.input();
b.volume();
cout << "----------------------------" << endl;
//T3
int n;
double s;
cout << "请输入学生人数:";
cin >> n;
Student a;
for (int i = 1;i <= n;i++)
{
cout << "请输入第" << i << "位学生的成绩:";
cin >> s;
a.scoretotalcount(s);
}
cout << "全体学生总分为:" << a.sum() << endl;
cout << "全体学生平均分为:" << a.average() << endl;
cout << "----------------------------" << endl;
//T4
point p1;
line l1(1, 2), l2(3, 4);
cout << "直线l1为:";
l1.print();
cout << "直线l2为:";
l2.print();
p1 = setPoint(l1, l2);
cout << "两直线的交点为:(" << p1.x << "," << p1.y << ")" << endl;
return 0;
}
//简陋的复数相加。。。几天没打忘光咯
//注意复制构造函数的调用
#include <iostream>;
using namespace std;
class Complex
{
public:
Complex(double,double);
Complex(const Complex& c);
void JIA(const Complex c);
void Input();
double sum1,sum2;
private:
double c1, c2;
};
Complex::Complex(double c1a = 0,double c2a=0)
{
c1 = c1a;
c2 = c2a;
}
Complex::Complex(const Complex& c)
{
c1 = c.c1;
c2 = c.c2;
cout << "hihi" << endl;
}
void Complex::JIA(const Complex c)
{
sum1 = c1 + c.c1;
sum2 = c2 + c.c2;
cout << sum1 << "+" << sum2 << "i" << endl;
}
void Complex::Input()
{
cin >> c1 >> c2;
}
int main()
{
Complex a,b;
a.Input();
b.Input();
a.JIA(b);
}