C++面向对象的三大特征
封装-类
具有相同性质的对象可以抽象为类,可以有属性,行为 类中的属性和行为统一称作成员 属性:成员变量,成员属性 行为:成员函数,成员方法
# include <iostream>
using namespace std;
const double PI = 3.14 ;
class Circle
{
public :
int r;
double cal_c ( )
{
return 2 * PI * r;
}
} ;
void main ( )
{
Circle c1;
c1. r = 5 ;
double c = c1. cal_c ( ) ;
cout << c << endl;
system ( "pause" ) ;
}
# include <iostream>
using namespace std;
class Student
{
public :
string name;
int id;
void show_name ( )
{
cout << name << endl;
}
void show_id ( )
{
cout << id << endl;
}
} ;
void main ( )
{
Student s1;
s1. name = "Zhangsan" ;
s1. id = 20 ;
s1. show_name ( ) ;
s1. show_id ( ) ;
system ( "pause" ) ;
}
封装-访问权限
public –成员在类内可以访问,在内外也可以访问 protected –成员在类内可以访问,在内外不可以访问,子类可以访问 private –成员在类内可以访问,在内外不可以访问,子类不可以访问
code:
# include <iostream>
using namespace std;
class Person
{
public :
string name;
protected :
string car;
private :
int password;
public :
void set_info ( string ref_name, string ref_car, int ref_password)
{
name = ref_name;
car = ref_car;
password = ref_password;
}
void show_info ( )
{
cout << name << "_" << car << "_" << password << endl;
}
} ;
void main ( )
{
Person p1;
p1. set_info ( "Lili" , "BMW" , 666888 ) ;
p1. show_info ( ) ;
p1. name = "HanMeimei" ;
p1. show_info ( ) ;
p1. set_info ( "GuTianle" , "Volvo" , 123456 ) ;
p1. show_info ( ) ;
system ( "pause" ) ;
}
result:
Lili_BMW_666888
HanMeimei_BMW_666888
GuTianle_Volvo_123456
封装-struct和class的区别
struct和class的默认访问权限不同。 – struct默认访问权限是public。 – class默认访问权限是private。
# include <iostream>
using namespace std;
class Person_c
{
string name;
void show_info ( )
{
cout << name << endl;
}
} ;
struct Person_s
{
string name;
void show_info ( )
{
cout << name << endl;
}
} ;
void main ( )
{
Person_c pc1;
Person_s ps1;
ps1. name = "Libai" ;
ps1. show_info ( ) ;
system ( "pause" ) ;
}
封装-成员属性私有化
成员属性设置为私有,可以控制其读写权限 对于写权限,可以检测数据有效性
# include <iostream>
using namespace std;
class Person
{
private :
string name;
int age = 0 ;
public :
void set_info ( string ref_name, int ref_age)
{
name = ref_name;
if ( ref_age < 0 || ref_age > 150 )
{
cout << "这是个妖怪!" << endl;
return ;
}
age = ref_age;
}
void show_info ( )
{
cout << name << "_" << age << endl;
}
} ;
void main ( )
{
Person p1;
p1. set_info ( "Libai" , 180 ) ;
p1. show_info ( ) ;
system ( "pause" ) ;
}
封装-成员变量为类
code:
# include <iostream>
using namespace std;
class Point
{
private :
int m_x;
int m_y;
public :
void set_info ( int x, int y)
{
m_x = x;
m_y = y;
}
void get_info ( int & x, int & y)
{
x = m_x;
y = m_y;
}
} ;
class Circle
{
private :
Point m_center;
int m_radius;
public :
void set_info ( Point center, int radius)
{
m_center = center;
m_radius = radius;
}
void get_info ( int & x, int & y, int & radius)
{
m_center. get_info ( x, y) ;
radius = m_radius;
}
} ;
void is_incricle ( Circle& c, Point& p)
{
int c_x, c_y, c_r;
c. get_info ( c_x, c_y, c_r) ;
int p_x, p_y;
p. get_info ( p_x, p_y) ;
int dis = ( p_x - c_x) * ( p_x - c_x) + ( p_y - c_y) * ( p_y - c_y) ;
if ( dis > ( c_r * c_r) )
cout << "点在圆外" << endl;
else if ( dis == ( c_r * c_r) )
cout << "点在圆上" << endl;
else
cout << "点在圆内" << endl;
}
int main ( )
{
Point p1;
p1. set_info ( 10 , 9 ) ;
int p_x, p_y;
p1. get_info ( p_x, p_y) ;
cout << "点的坐标是:" << "x=" << p_x << " y=" << p_y << endl;
Point p2;
p2. set_info ( 10 , 0 ) ;
Circle c1;
c1. set_info ( p2, 10 ) ;
int x, y, r;
c1. get_info ( x, y, r) ;
cout << "圆的坐标是:" << "x=" << x << " y=" << y << " r=" << r << endl;
is_incricle ( c1, p1) ;
system ( "pause" ) ;
}
result:
点的坐标是:x= 10 y= 9
圆的坐标是:x= 10 y= 0 r= 10
点在圆内
封装-将类置于单独的文件中
# pragma once
# include <iostream>
using namespace std;
class Point
{
private :
int m_x;
int m_y;
public :
void set_info ( int x, int y) ;
void get_info ( int & x, int & y) ;
} ;
point. cpp文件
# include "point.h"
void Point :: set_info ( int x, int y)
{
m_x = x;
m_y = y;
}
void Point :: get_info ( int & x, int & y)
{
x = m_x;
y = m_y;
}
circle. h文件
# pragma once
# include <iostream>
using namespace std;
# include "point.h"
class Circle
{
private :
Point m_center;
int m_radius;
public :
void set_info ( Point center, int radius) ;
void get_info ( int & x, int & y, int & radius) ;
} ;
# include "circle.h"
void Circle :: set_info ( Point center, int radius)
{
m_center = center;
m_radius = radius;
}
void Circle :: get_info ( int & x, int & y, int & radius)
{
m_center. get_info ( x, y) ;
radius = m_radius;
}