Advertisement

第一次课堂笔记

阅读量:

@第一次课堂笔记

C++语言程序设计——类与对象

一、抽象:是对具体对象(问题)进行概括,抽出这一类对象的公共性质并加以描述的过程。

抽象例子:钟表

数据抽象:

int Hour,int Minute,int Second

代码抽象:

SetTime(),ShowTime()

class Clock

{

public:

void SetTime(int NewH,int NewM,int NewS);

void ShowTime();

private:

int Hour,Minute,Second;

};

二、类的声明形式

类是一种用户自定义类型,声明形式:

class 类名称

{

public:

复制代码
         公有成员(外部接口)
    
    
      
    

private:

复制代码
         私有成员
    
    
      
    

protected:

复制代码
         保护型成员
    
    
      
    

};

公有成员:在关键字public后面声明,它们是类与外部的接口,任何外部函数都可以访问公有类型数据和函数。

私有成员:在关键字private后面声明,只允许本类中的函数访问,而类外部的任何函数都不能访问

三、内联成员函数

(1)内联函数体中不要有复杂结构(如循环语句和switch语句)。

(2)在类中声明内联成员函数的方式:

·将函数体放在类的声明中。

·使用inline关键字。

例子:

class Point

{

public:

复制代码
    void Init(int initX,int initY)
    
    {
    
      X=initX;
    
      Y=initY;
    
    }
    
    int GetX() {return X;}
    
    int GetY() {return Y;}
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    

private:

复制代码
    int X,Y;
    
    
      
    

};

四、对象

类的对象是该类的某一特定实体,即类类型的变量。

声明形式:

类名 对象名;

例:

Clock
myClock;

访问方式:

(1)类中成员互访:直接使用成员名

(2)类外访问:使用“对象名.成员名”方式访问 public 属性的成员

五、构造函数

class Clock

{

public:

复制代码
    Clock(int NewH,int NewM,int NewS);//构造函数
    
    void SetTime(int NewH,int NewM,int NewS);
    
    void ShowTime();
    
    
      
      
      
      
      
    

private:

复制代码
    int Hour,Minute,Second;
    
    
      
    

};

构造函数的实现:

Clock::Clock(int
NewH, int NewM, int NewS)

{

复制代码
    Hour= NewH;
    
    Minute= NewM;
    
    Second= NewS;
    
    
      
      
      
      
      
    

}

建立对象时构造函数的作用:

int main()

{

Clock c(0,0,0); //隐含调用构造函数,将初始值作为实参。

c.ShowTime();

}

六、函数重载

重载函数的声明

C++允许功能相近的函数在相同的作用域内以相同函数名声明,从而形成重载。方便使用,便于记忆。

例:

int add(int x,int
y);

float add(float
x,float y);

(形参类型不同)

int add(int x,int
y);

int add(int x,int
y, int z);

(形参个数不同)
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
例 重载函数应用举例

编写三个名为add的重载函数,分别实现两整数相加、两实数相加和两个复数相加的功能。

#include

using namespace
std;

struct complex

{

复制代码
    double real;
    
    double imaginary;
    
    
      
      
      
    

};

int main()

{

复制代码
    int m, n;
    
    double x, y;
    
    complex c1, c2, c3;
    
    int add(int m, int n);
    
    double add(double x, double y);
    
    complex add(complex c1, complex c2);
    
    
    
    cout<<"Enter two integer:
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

";

复制代码
    cin>>m>>n;
    
    cout<<"integer
    
    
      
      
      
    

<<m<<’+’<<n<<"="<<add(m,n)<<endl;

复制代码
    cout<<"Enter two real number:
    
    
      
    

";

复制代码
    cin>>x>>y;
    
    cout<<"real number
    
    
      
      
      
    

“<<x<<’+’<<y<<”= "<<add(x,y)
<<endl;

复制代码
    cout<<"Enter the first
    
    
      
    

complex number: ";

复制代码
    cin>>c1.real>>c1.imaginary;
    
    cout<<"Enter the second
    
    
      
      
      
    

complex number: ";

复制代码
    cin>>c2.real>>c2.imaginary;
    
    c3=add(c1,c2);
    
    cout<<"complex number
    
    
      
      
      
      
      
    

("<<c1.real<<’,’

复制代码
      <<c1.imaginary
    
    
      
    

<<")+("<<c2.real<<’,’

<<c2.imaginary<<")=("<<c3.real<<’,’

<<c3.imaginary<<")\n";

}

int add(int m, int
n)

{ return m+n; }

double add(double
x, double y)

{ return x+y; }

complex add(complex
c1, complex c2)

{

complex c;

复制代码
    c.real=c1.real+c2.real;
    
    c.imaginary=c1.imaginary+c2.imaginary;
    
    return c;
    
    
      
      
      
      
      
    

}

运行结果:

Enter two integer:
3 5

integer 3+5=8

Enter two real
number: 2.3 5.8

real number
2.3+5.8= 8.1

Enter the first
complex number: 12.3 45.6

Enter the second
complex number: 56.7 67.8

complex number
(12.3,45.6)+(56.7,67.8)= (69,113.4)

一圆形游泳池如图所示,现在需在其周围建一圆形过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
在这里插入图片描述

#include

using namespace
std;

const float PI =
3.14159;

const float
FencePrice = 35;

const float
ConcretePrice = 20;

//声明类Circle 及其数据和方法

class Circle

{

private:

复制代码
    float  
    
    
      
    

radius;

public:

复制代码
    Circle(float r);  //构造函数
    
    
    
    float Circumference(); //圆周长
    
    float Area();  //圆面积
    
    
      
      
      
      
      
      
      
    

};

// 类的实现

// 构造函数初始化数据成员radius

Circle::Circle(float
r)

{radius=r;}

// 计算圆的周长

float
Circle::Circumference()

{

复制代码
    return 2 * PI * radius;
    
    
      
    

}

// 计算圆的面积

float
Circle::Area()

{

复制代码
    return PI * radius * radius;
    
    
      
    

}

void main ()

{

float radius;

float FenceCost, ConcreteCost;

// 提示用户输入半径

cout<<"Enter the radius of the
pool: ";

cin>>radius;

// 声明 Circle 对象

Circle Pool(radius);

Circle PoolRim(radius + 3);

//计算栅栏造价并输出

FenceCost=PoolRim.Circumference()*FencePrice;

cout<<“Fencing Cost is ¥”<<FenceCost<<endl;

//计算过道造价并输出

ConcreteCost=(PoolRim.Area()-

Pool.Area())*ConcretePrice;

cout<<“Concrete Cost is ¥”<<ConcreteCost<<endl;

}

运行结果

Enter the radius of
the pool: 10

Fencing Cost is ¥2858.85

Concrete Cost is ¥4335.39

全部评论 (0)

还没有任何评论哟~