C++面向对象基础入门
面向对象三大特性:封装 继承 多态
封装一个类
const double pi=3.14;
class circle//包括属性,行为
{
public://公共权限 struct默认权限
int m_r;
string m_name;
int date[3];
double calculate()
{
return 2*pi*m_r;
}
void print()
{
cout<<m_name<<endl;
cout<<car<<endl;
}
void settime(int b[3])
{
date[0]=b[0];
date[1]=b[1];
date[2]=b[2];
}
//privade:(class默认权限)
protected:
string car="拖拉机";
};
int main()
{
circle c1;//实例化一个对象
c1.m_r=10;
c1.m_name="baishaohua";
cout<<"圆的周长是"<<c1.calculate()<<endl;
c1.print();
//c1.car="benzi";访问不了
// cout<<c1.car<<endl;
int b[3]={2021,1,20};
c1.settime(b);
cout<<c1.date[0]<<endl;
cout<<c1.date[1]<<endl;
cout<<c1.date[2]<<endl;
}
输出为
圆的周长是62.8
baishaohua
拖拉机
2021
1
20
这里注意,class感觉就是高级版的结构体,他们的区别是class默认的是private,不可以在类外访问到,必须加上public才可以被访问,而结构体默认的就是public,可以直接在类外访问
成员属性私有化
用类中的函数访问私有属性,可以判断数据的有效性
class person
{
public:
void setname(string name)
{
string str=name;
cout<<str<<endl;
cout<<sizeof(str)<<endl;
cout<<sizeof(char)<<endl;
if((sizeof(str)/sizeof(char))<8*5)
cout<<"名字太短换了吧"<<endl;
else
m_name=name;
}
string getname()
{
return m_name;
}
private:
string m_name;
int m_age;
string m_lover;
};
int main()
{
person p;
p.setname("bai");
cout<<p.getname()<<endl;
}
列表初始化赋初始值
class person
{
public:
person(int a,int b,int c):m_1(a),m_2(b),m_3(c)
{
}
int m_1;
int m_2;
int m_3;
};
int main()
{
person p(30,20,10);
cout<<p.m_1<<endl;
cout<<p.m_2<<endl;
cout<<p.m_3<<endl;
}
注意这里的person是一个构造函数中的有参构造,用它对类的属性赋值是相当的方便。
构造函数与析构函数
class person
{
public:
person(int a):m_1(a)//构造函数 有参构造和无参构造(默认)
{
cout<<"有参构造函数调用"<<endl;
}
person()//构造函数 有参构造和无参构造(默认)
{
cout<<"无参构造函数调用"<<endl;
}
~person()
{
cout<<"析构函数调用"<<endl;
}
person(const person &p)//拷贝构造函数 区别于普通构造函数
{
cout<<"拷贝构造函数调用"<<endl;
m_1=p.m_1;//将传入的类的m_1属性复制到该对象中
}
int m_1;
};
int main()
{
//调用方法 1.括号法 2.显示法 3.隐式转换法
person p1;
person p2(10);
person p3(p2);
cout<<"p2"<<p2.m_1<<endl;
cout<<"p3"<<p3.m_1<<endl;
}
只要实例化一个类都会调用构造函数和析构函数
此外,静态成员变量不属于任意一个类
空对象所占内存为1
成员函数与成员变量分开存储 非静态成员函数不属于类对象上 只有非静态成员变量属于类的对象上
cout<<运算符重载
class person
{
public:
person(int a,int b,int c):m_1(a),m_2(b),m_3(c)
{
}
int m_1;
int m_2;
int m_3;
};
void operator<<(ostream &cout,person &p)
{
cout<<" m_1="<<p.m_1 <<endl;
cout<<" m_2="<<p.m_2 <<endl;
cout<<" m_3="<<p.m_3 <<endl;
}
int main()
{
person p(30,20,10);
cout<<p;
}
输出:
m_1=30
m_2=20
m_3=10
注意这里的输出,是不是感觉C++可以随心所欲?
继承
class common
{
public:
int feihuoliang;
int height;
int weight;
};
class man:public common
{
public:
double onekeiltime;
int quantity_of_yiti;
};
class woman:public common
{
public:
double eight_H_time;
int quantity_of_yangwo;
};
void setvalue(man&p1,woman&p2)
{
p1.feihuoliang=4000;
p1.height=178;
p1.onekeiltime=3.50;
p1.quantity_of_yiti=20;
p1.weight=68;
p2.weight=100;
p2.quantity_of_yangwo=78;
p2.height=168;
p2.feihuoliang=3600;
p2.eight_H_time=4.00;
}
void getvalue(man&p1,woman&p2)
{
cout<<"The man p1's value is as follows"<<endl;
cout<<p1.feihuoliang<<endl;
cout<< p1.height<<endl;
cout<<p1.onekeiltime<<endl;
cout<<p1.quantity_of_yiti<<endl;
cout<<p1.weight<<endl;
cout<<"________********______"<<endl;
cout<<"The woman p2's value is as follows"<<endl;
cout<<p2.feihuoliang<<endl;
cout<< p2.height<<endl;
cout<<p2.eight_H_time<<endl;
cout<<p2.quantity_of_yangwo<<endl;
cout<<p2.weight<<endl;
}
int main()
{
man p1;
woman p2;
setvalue(p1,p2);
getvalue(p1,p2);
}
输出:
The man p1's value is as follows
4000
178
3.5
20
68
________********______
The woman p2's value is as follows
3600
168
4
78
100
动态多态
class animal
{
public:
virtual void speak() //virtual实现虚函数,使其地址晚绑定
{
cout<<"动物在说话"<<endl;
}
};
class Cat:public animal
{
public:
void speak()
{
cout<<"小猫在说话"<<endl;
}
};
void dospeak(animal &animal)
{
animal.speak();
}
int main()
{
Cat cat;
dospeak(cat);
}
输出:
小猫在说话
关键字vertual虚函数实现调用子类函数
自此,简单的类和面向对象就学完了,嘿嘿!!