0%

数据结构与算法之面向对象与流

本节主要介绍了C++中面向对象与流的最基本操作。

类的定义与使用

  • 看上去像“带函数的结构体”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
using namespace std;

//类的定义
// class Rectangle{
// public:
// int w,h;//成员变量
// int Area()//成员函数:求面积
// {
// return w*h;
// }
// int Perimeter()//成员函数:求周长
// {
// return 2*(w+h);
// }
// void Init(int w_,int h_)//成员函数:设置长宽
// {
// w=w_;h=h_;
// }
// };//必须有分号

//声明与定义分离的定义方式
class Rectangle{
public:
int w,h;
int Area();
int Perimeter();
void Init(int w_,int h_);
};

int Rectangle::Area()
{
return w*h;
}

int Rectangle::Perimeter()
{
return 2*(w+h);
}

void Rectangle::Init(int w_,int h_)
{
w=w_;
h=h_;
}

int main(void)
{
int w,h;
//类的使用
//1.对象名.成员名
Rectangle r;//r是一个对象
cout<<"Please enter the w and h:";
cin>>w>>h;
r.Init(w,h);
cout<<r.Area()<<endl<<r.Perimeter()<<endl;
cout<<"--------------------------------------"<<endl;
//2.指针->成员名
Rectangle r1,r2;
Rectangle *p1=&r1;
Rectangle *p2=&r2;
p1->w=5;
p1->h=6;
p2->Init(5,4);
cout<<"p1->Area() is:"<<p1->Area()<<","<<"p1->Perimeter() is:"<<p1->Perimeter()<<endl;
cout<<"p2->Area() is:"<<p2->Area()<<","<<"p2->Perimeter() is:"<<p2->Perimeter()<<endl;
cout<<"--------------------------------------"<<endl;
//3.引用名.成员名
Rectangle &rr=r2;//rr相当于r2的别名
rr.w=5;rr.h=3;
cout<<rr.Area()<<endl<<r2.Perimeter()<<endl;
return 0;
}

结果如下:

image-20221206141613337

  • 访问权限:
    • private:私有成员,只能在成员函数中访问(默认)
    • public:共有成员,可以在任何地方访问
    • protected:保护成员

1.标准输入流

  • cin>>x;

    • 读入整数时以第一个非数字为终结
    • 读入字符串时以第一个空格、tab或换行符为终结
  • cin.getline(str,len,ch);

    • 读入一个字符串
    • ch被从流中提出,不存入str
  • ch=cin.get();

    • 读入一个单独的字符
  • cin.ignore(int n =1, int delim = EOF)

    • 此函数的作用是跳过输入流中的 n 个字符,或跳过 delim 及其之前的所有字符,哪个条件先满足就按哪个执行
    • 两个参数都有默认值,因此 cin.ignore() 就等效于 cin.ignore(1, EOF), 即跳过一个字符
  • cin.width(num)

    • 设置域宽
    • 宽度为num
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include<iostream>
    #include<string>
    using namespace std;

    int main(void)
    {
    string string;
    cin.width(5);
    cin>>string;
    cout<<string<<endl;
    cin>>string;
    cout<<string<<endl;
    return 0;
    }

    结果如下:

    image-20221206150213207

2.标准输出流

  • cout<<y;

  • cout.put('A').put('a')//使用put输出一个字符

  • 控制输出浮点数的精度

    • 头文件:#include<iomanip>
    • 精度设置:setprecision(num)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include<iostream>
    #include<iomanip>
    using namespace std;

    int main(void)
    {
    double x=1234567.89,y=12.34567;
    int n=1234567;
    int m=12;
    //setiosflags(ios::fixed)表示设置成小数的形式
    //此时setprecision(6)表示保留小数点后6位
    //若只有setprecision(6)作用,则表示整数和小数一共占6位
    cout<<setiosflags(ios::fixed)<<setprecision(6);
    cout<<"x:"<<x<<endl;
    cout<<"y:"<<y<<endl;
    cout<<"n:"<<n<<endl;//精度的设置对整数不起作用
    cout<<"m:"<<m<<endl;
    return 0;
    }

    结果如下:

    image-20221206150120684

欢迎来到ssy的世界