面向对象面向对象的方法求圆的面积
- 面向对象的方法
1.类的抽象 成员变量和成员函数
2.实例化
3.求面积,面向过程加工是一个一个的函数,面向对象是一个一个的类
方法2:用面向对象方法编程,求圆的周长和面积
#include<iostream.h>
using name std;
class Circle
{ double radius ; //成员变量
public : //类的访问控制
void Set_Radius( double r ) { radius = r ; } //成员函数
double Get_Radius() { return radius ; } //通过成员函数设置成员变量
double Get_Girth() { return 2 * 3.14f * radius ; } //通过成员函数获取成员变量
double Get_Area() { return 3.14f * radius * radius ; }
} ;
void main()
{
Circle A, B ; //用类定义对象
A.Set_Radius( 6.23 ) ; //类的调用
cout << "A.Radius = " << A.Get_Radius() << endl ;
cout << "A.Girth = " << A.Get_Girth() << endl ;
cout << "A.Area = " << A.Get_Area() << endl ;
B.Set_Radius( 10.5 ) ;
cout << "B.radius = " << B.Get_Radius() << endl ;
cout << "B.Girth=" << B.Get_Girth() << endl ;
cout << "B.Area = " << B.Get_Area() << endl ;
}
总结:建立类、对象、成员变量、成员函数,输入输入流基本概念。
容易出现的问题: