C++ <二>
#include<iostream>
using namespace std;
class A{
int n;
const int m;
public:
// A():n(0),m(9){// 常量只能被初始化不能赋值。
//不能用来初始化数组和结构化变量。
//cout<<"call A()"<<this<<endl;
// }
A(int n=0,int m=0):n(n),m(m){
cout<<"call A(int)"<<this<<endl;
}
void show(){
cout<<"n="<<n<<",m="<<m<<endl;
}
};
int main()
{
A a;
A a1(100,12);
a.show();
a1.show();
}
*/
/*
#include<iostream>
using namespace std;
class Int{//子类的构造函数最先调用, 析构函数最后调用
public:
Int(){//类型是类的变量时对象, 创建对象的时候会自动调用构造函数,
//全局对象的构造函数在main函数之前调用。
cout<<"Int()"<<endl;
}
~Int(){cout<<"~Int()"<<endl;
}
};
class A{
int n;
Int obj;
public:
A(int n=0):n(n){
cout<<this<<"A("<<n<<")"<<endl;
}
~A(){//总是无形参的,故也不能重载。
cout<<this<<"~A()"<<n<<endl;
}//没写构造函数和析构函数的时候系统默认执行。
//自己写了构造函数后系统不再提供构造函数和析构函数。
};
int main()
{
A a1(1);//释放对象的时候调用。全局变量最先创建,最后释放。
}
// day8_ pm _196
*/
/*
//作业:写一个Book类,数据有书名和数量、价格。函数有一个构造函数(形参是书名,数量,价格);
//和一个无参的构造函数(把数量和价格设置为0,书名设置为“无名”),
//再写一个sale函数(形参有数量,显示卖了多少钱,同时减少书的数量,如果数量不够的时候,输出一个错误信息),
//还有一个show函数来显示书名、价格和数量。为了保障保证无参方式创建的对象也能够实用
//,再写一个set函数(书名,价格,数量),在main中实用它。
#include<iostream>
using namespace std;
#include<cstring>
class Book{
char name[20];
float amount;
float price;
public:
Book(char name[20],float amount,float price):amount(amount),price(price){
for(int i=0;i<20;i++)
{
this->name[i]=name[i];
}
}
Book():amount(0),price(0){
strcpy(name,"no name\0");
//this->name[0]='n';
//this->name[1]='o';
//this->name[2]=' ';
//this->name[3]='n';
//this->name[4]='a';
//this->name[5]='m';
//this->name[6]='e';
//this->name[7]='\0';
}
void sale(float amount );
void show();
void set(char name[20],float price,float amount);
};
int main()
{
Book My_Book;
My_Book.show();
My_Book.set("百年孤独",33.2,4500);
Book R("平凡的世界",500,23.5);
R.show();
My_Book.sale(100);
My_Book.show();
R.sale(234);
R.show();
}
void Book::sale(float amount)
{
this->amount-=amount;
cout<<"价格:"<<price<<"-收入:"<<amount*price<<"-库存:"<<this->amount<<endl;
}
void Book::show()
{
cout<<"书名:"<<name<<" 价格:"<<price<<" 库存:"<<amount<<endl;
}
void Book::set(char name[20],float price,float amount)
{
for(int i=0;i<20;++i)
{
this->name[i]=name[i];
}
this->price=price;
this->amount=amount;
}
*/
//写一个分数类,数据有分子分母,函数有构造函数(形参是分子分母,默认值分别为0和1),
//一个约分函数(无参),一个show函数来输出这个分数, 一个Value函数来返回这个分数对应的小数值。在main函数中使用它。
//面向对象
//如何定义和实现一个类,类也是一种数据类型, 创建对象。
//在旧知识的基础上学习新知识。
//构造函数,初始化
//适用对象,对象.成员函数(实参);析构函数(可以不用),
/*
#include<iostream>
using namespace std;
class Person{
char name[20];
bool gender;
int age;
public:
Person(char* name,bool gender,int age=0):gender(gender),age(age){
if(strlen(name)>19){
name[19]='\0';
cout<<" 名字太长!!"<<endl;
}
strcpy(this->name,name);
}
void setAge(int age){
if(age<0)cout<<"年龄不能小于零!!"<<endl;
else if(age>200) cout<<"年龄太大!!"<<endl;
else
this->age=age;
}
void show(){
cout<<"姓名:"<<name<<" 性别:"<<(gender?"男": "女")<<" 年龄:"<<age<<endl;
}
};
int main()
{
Person p1("胡杨",1,18);
p1.show();
Person p2("姚笛",0,23);
p2.show();
}
*/
//继承和多态
//约分类,从最大开始约分。
/*
#include<iostream>
using namespace std;
class Person{
protected:
char name[20];
bool gender;
int age;
public:
Person(char* name,bool gender,int age=0):gender(gender),age(age){
if(strlen(name)>19){
name[19]='\0';
cout<<" 名字太长!!"<<endl;
}
strcpy(this->name,name);
}
void setAge(int age){
if(age<0)cout<<"年龄不能小于零!!"<<endl;
else if(age>200) cout<<"年龄太大!!"<<endl;
else
this->age=age;
}
void show(){
cout<<"姓名:"<<name<<" 性别:"<<(gender?"男": "女")<<" 年龄:"<<age<<endl;
}
};
class Student: public Person{//继承
char xuehao[10];
double score;
public:
Student(char* name,bool gender,int age=0):Person( name, gender, age){
if(strlen(name)>19){
name[19]='\0';
cout<<" 名字太长!!"<<endl;
}
strcpy(this->name,name);
}
void setXuehao(char xuehao[10]){
strcpy(this->xuehao,xuehao);
}
void setScore(double score){
this->score=score;
}
void show(){
Person::show();
cout<<"学号: "<<xuehao<<"成绩 "<<score<<endl;
}
};
int main()
{
Person p1("胡杨",1,18);
p1.show();
Person p2("姚笛",0,23);
p2.show();
Student a1("刘毅",1,22);
a1.setScore(90);
a1.setXuehao("074514");
a1.show();
Student a2("海军",1,19);
a2.setAge(19);
a2.setScore(89.5);
a2.setXuehao("1233434");
a2.show();
}
/*
#include<iostream>
using namespace std;
class A{
int data;
public:
A():data(0){
cout<< "A()"<<endl;
}
A(int d):data(d){
cout<<"A(int)"<<data<<endl;
}
~A(){
cout<<"~A()"<<endl;
}
void show(){
cout<< data<<endl;
}
};
int main()
{
A a1(1);
A(2);//没有名字的对象、调用后立即释放, 没有名字不能再次使用
cout<<"____________"<<endl;
A(3).show();
A a2;
a2=A(4);//赋值,等号两边类型一致,??强制类型转换??
(A)5;//强制类型转换, 注意与构造函数形参要一致。
a2=40;//类型向高级自动转化//a2=(A)40;or a4=A(40),把其他类型转换成类的类型。
}
*/
/*
#include<iostream>
using namespace std;
class A{
int data;
public:
A():data(0){
cout<< "A()"<<endl;
}
A(int d):data(d){
cout<<"A(int)"<<data<<endl;
}
~A(){
cout<<"~A()"<<endl;
}
void show(){//优先调用普通函数, 重载。
cout<<"DATA="<<data<<endl;
}
void show() const{//声明这个成员函数不会修改当前对象的数据。
cout<<"data="<< data<<endl;
}
};
int main()
{
A a1(1);
const A a2(2);
a1.show();//优先调用普通函数
a2.show();//const对象不能调用普通成员函数, 需将函数做特殊说明,
}
*/
//代分数类,学生类.
//分数类派生代分数类
/*
#include<iostream>
using namespace std;
class Fenshu{
protected:
int fz,fm;
public:
Fenshu(int fz=0,int fm=0){
cout<<"请输入分子、分母:";
cin>>fz>>fm;
this->fz=fz;
this->fm=fm;
cout<<endl;
}
void yuefen(){
for(int i=fz;i>1;--i){
if(fz%i==0&fm%i==0)
{
fz/=i;
fm/=i;
}
}
}
void show(){
cout<<"约分后的分数为:("<<fz<<"/"<<fm<<")"<<endl;
}
};
class DFenshu:public Fenshu{
int Int;
public:
void value(){
Int=fz/fm;
fz-=fm*Int;
}
void show(){
cout<<"约分后分数为:"<<Int;
if(fz!=0) cout<<"("<<fz<<"/"<<fm<<")"<<endl;
}
};
int main()
{
Fenshu a;
a.yuefen();
a.show();
DFenshu b;
b.yuefen();
b.value();
b.show();
}
*/