例2.1

使用成员函数的实例。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct Point
  8. {
  9. void Setxy(double a, double b)//成员函数,用来重新设置数据成员
  10. {
  11. x = a;
  12. y = b;
  13. }
  14.  
  15. void Display()//成员函数,按指定格式输出数据成员的值
  16. {
  17. cout << x << "\t" << y << endl;
  18. }
  19.  
  20. double x, y;//数据成员
  21. };
  22.  
  23. void main()
  24. {
  25. Point a;//定义对象a
  26.  
  27. a.Setxy(10.6, 18.5);//设置对象a的数据成员
  28. a.Display();//显示对象a的数据成员
  29.  
  30. cout << a.x << "\t" << a.y << endl;
  31. }

例2.2

使结构具有封装性的实例。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct Point
  8. {
  9. private:
  10. double x, y;//数据成员
  11.  
  12. public:
  13. void Setxy(double a, double b)//成员函数,用来重新设置数据成员
  14. {
  15. x = a;
  16. y = b;
  17. }
  18.  
  19. void Display()//成员函数,按指定格式输出数据成员的值
  20. {
  21. cout << x << "\t" << y << endl;
  22. }
  23. };
  24.  
  25. void main()
  26. {
  27. Point a;
  28.  
  29. a.Setxy(10.6, 18.5);
  30.  
  31. a.Display();
  32.  
  33. //cout << a.x;
  34.  
  35. //1>------已启动生成: 项目: hello, 配置 : Debug Win32------
  36. // 1> main.cpp
  37. // 1>c:\users\denggl18.gdctc\documents\visual studio 2015\projects\hello\hello\main.cpp(33) : error C2248 : “Point::x” : 无法访问 private 成员(在“Point”类中声明)
  38. // 1> c:\users\denggl18.gdctc\documents\visual studio 2015\projects\hello\hello\main.cpp(10) : note : 参见“Point::x”的声明
  39. // 1> c:\users\denggl18.gdctc\documents\visual studio 2015\projects\hello\hello\main.cpp(8) : note : 参见“Point”的声明
  40. // == == == == == 生成 : 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 == == == == ==
  41.  
  42. }

例2.3

使用构造函数初始化对象的实例。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct Point
  8. {
  9. private:
  10. double x, y;//数据成员
  11.  
  12. public:
  13. Point()//无参数构造函数
  14. {
  15.  
  16. };
  17.  
  18. Point(double a, double b)//具有两个参数的构造函数
  19. {
  20. x = a;
  21. y = b;
  22. }
  23.  
  24. void Setxy(double a, double b)//成员函数,用来重新设置数据成员
  25. {
  26. x = a;
  27. y = b;
  28. }
  29.  
  30. void Display()//成员函数,按指定格式输出数据成员的值
  31. {
  32. cout << x << "\t" << y << endl;
  33. }
  34. };
  35.  
  36. void main()
  37. {
  38. Point a;//定义对象a
  39. Point b(18.5, 10.6);//定义对象b并赋初值
  40.  
  41. a.Setxy(10.6, 18.5);//设置变量a的数据成员
  42.  
  43. a.Display();//显示变量a的数据成员
  44. b.Display();//显示变量b的数据成员
  45. }

例2.4

定义类的实例。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Point
  8. {
  9. private:
  10. double x, y;//类Point的数据成员
  11.  
  12. public:
  13. Point()//类Point的无参数构造函数
  14. {
  15.  
  16. };
  17.  
  18. Point(double a, double b)//具有两个参数的构造函数
  19. {
  20. x = a;
  21. y = b;
  22. }
  23.  
  24. void Setxy(double a, double b)//成员函数,用来重新设置数据成员
  25. {
  26. x = a;
  27. y = b;
  28. }
  29.  
  30. void Display()//成员函数,按指定格式输出数据成员的值
  31. {
  32. cout << x << "\t" << y << endl;
  33. }
  34. };
  35.  
  36. void main()
  37. {
  38. Point a;//定义类Point的对象a
  39. Point b(18.5, 10.6);//定义类Point的对象b并初始化
  40.  
  41. a.Setxy(10.6, 18.5);//为对象a的数据成员赋值
  42.  
  43. a.Display();//显示对象a的数据成员
  44. b.Display();//显示对象b的数据成员
  45. }

例2.8

演示使用string对象及初始化的例子。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void main()
  9. {
  10. string str1("We are here!");
  11. string str2 = "Where are you?";
  12.  
  13. cout << str1[] << str1[] << "," << str1 << endl;
  14. cout << str2[] << str2[] << "," << str2 << endl;
  15. cout << "please input word:";
  16.  
  17. cin >> str1;
  18.  
  19. cout << "length of the " << str1 << " is " << str1.size() << "." << endl;
  20. }

例2.9

演示将美国格式的日期转换为国际格式的例子。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void main()
  9. {
  10. cout << "Enter the date in American format"
  11. << "(e.g., December 25, 2002):";
  12.  
  13. string Date;
  14.  
  15. getline(cin, Date, '\n');
  16.  
  17. int i = Date.find(" ");
  18.  
  19. string Month = Date.substr(, i);
  20.  
  21. int k = Date.find(",");
  22.  
  23. string Day = Date.substr(i + , k - i - );
  24. string Year = Date.substr(k + , );
  25. string NewDate = Day + " " + Month + " " + Year;
  26.  
  27. cout << "Original date:" << Date << endl;
  28. cout << "Converted date:" << NewDate << endl;
  29. }

例2.10

演示使用complex和string对象及初始化的例子。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <complex>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void main()
  10. {
  11. complex <int> num1(, );
  12. complex <float> num2(3.5, 4.5);
  13.  
  14. string str1("real is ");
  15. string str2 = "image is ";
  16.  
  17. cout << str1 << num1.real() << ',' << str2 << num1.imag() << endl;
  18. cout << str1 << num2.real() << ',' << str2 << num2.imag() << endl;
  19. }

例2.11

演示string对象的例子。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <iterator>
  7.  
  8. using namespace std;
  9.  
  10. void main()
  11. {
  12. string str1 = "we are here!", str2 = str1;
  13.  
  14. reverse(&str1[], &str1[] + );//str1字符串的元素逆向
  15.  
  16. cout << str1 << endl;//输出逆向后的内容
  17.  
  18. copy(&str1[], &str1[] + , &str2[]);//原样复制到str2
  19.  
  20. cout << str2 << endl;//输出str2
  21.  
  22. reverse_copy(&str2[], &str2[] + , ostream_iterator<char>(cout));//逆向输出str2
  23. }

例2.12

演示string对象使用成员函数表示存储区间的例子。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <functional>
  7. #include <iterator>
  8.  
  9. using namespace std;
  10.  
  11. void main()
  12. {
  13. string str1 = "wearehere!", str2(str1);//使用str1初始化
  14.  
  15. reverse(str1.begin(), str1.end());//字符串元素逆向
  16.  
  17. cout << str1 << endl;
  18.  
  19. copy(str1.begin(), str1.end(), str2.begin());//原样复制到str2,str2应能容纳下str1
  20.  
  21. sort(str1.begin(), str1.end());//默认升幂排序
  22.  
  23. cout << str1 << endl;//输出排序结果
  24. cout << str2 << endl;//输出字串str2的内容
  25.  
  26. reverse_copy(str1.begin(), str1.end(), str2.begin());//逆向复制到字串str2的内容
  27.  
  28. cout << str2 << endl;//输出逆向后的str2的内容
  29.  
  30. reverse(str2.begin() + , str2.begin() + );//字串str2部分逆向
  31.  
  32. copy(str2.begin() + , str2.begin() + , ostream_iterator<char>(cout));//输出逆向后的部分内容
  33.  
  34. cout << endl;
  35.  
  36. sort(str1.begin(), str1.end(), greater<char>());//降幂排序
  37.  
  38. cout << str1 << endl;//输出排序后的字符str1
  39.  
  40. str1.swap(str2);//互相交换内容
  41.  
  42. cout << str1 << " " << str2 << endl;
  43. }

例2.13

演示string对象数组的例子。

  1. #define _SCL_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <iterator>
  7.  
  8. using namespace std;
  9.  
  10. void main()
  11. {
  12. string str[] = { "We are here!","Where are you?","welcome!" };
  13.  
  14. for (int i = ; i < ; i++)
  15. {
  16. copy(str[i].begin(), str[i].end(), ostream_iterator<char>(cout));
  17. cout << endl;
  18. }
  19.  
  20. str[].swap(str[]);
  21. str[].swap(str[]);
  22.  
  23. for (int i = ; i < ; i++)
  24. {
  25. cout << str[i] << endl;
  26. }
  27. }

04737_C++程序设计_第2章_从结构到类的演变的更多相关文章

  1. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  2. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  3. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  4. 04737_C++程序设计_第4章_类和对象

    例4.1 描述点的Point类. 例4.2 根据上面对Point类的定义,演示使用Point类的对象. #define _SCL_SECURE_NO_WARNINGS #include <ios ...

  5. 04737_C++程序设计_第3章_函数和函数模板

    例3.1 传对象不会改变原来对象数据成员值的例子. #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <str ...

  6. 04737_C++程序设计_第1章_认识C++的对象

    例1.1 演示使用结构对象的示例程序. //功能:将结构对象的两个域值相加,乘以2再加50 #include <iostream>//包含头文件 using namespace std;/ ...

  7. 04737_C++程序设计_第10章_面向对象设计实例

    10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...

  8. 04737_C++程序设计_第9章_运算符重载及流类库

    例9.1 完整实现str类的例子. #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> ...

  9. 04737_C++程序设计_第8章_多态性和虚函数

    例8.1 分析下面程序的输出结果. 例8.2 分别使用指针和引用的display函数. #include <iostream> using namespace std; const dou ...

随机推荐

  1. 自学nodejs系列

    好久没有搞nodejs了.nodejs也是更新的很快!!当初翻译的文档

  2. win7+64安装PLSQL Developer 32位

    原因分析:在网上搜索了半天,主要原因是oci.dll是64位的,而PL/SQL developer只有32位的,在使用64位oci.dll文件时出错! 解决方案(最便捷):1.到oracle官网下载O ...

  3. EF6.0 自定义Code First约定

    自定义Code First约定有三种方式,分别是:Lightweight Conventions(轻量级约定).Configuration Conventions(配置型约定).Model-based ...

  4. QT GUI(主)线程与子线程之间的通信——使用跨线程的信号槽

    在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...

  5. [Leetcode][Python]51: N-Queens

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...

  6. DBS小结

    <数据库系统原理>主要介绍的是数据库技术的基本原理.方法和应用技术. 它可以使我们能有效地使用现有的数据库管理系统和软件开发工具,掌握数据库结构的设计和数据库应用系统的开发原理. 在这里, ...

  7. [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化

    [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化 // point_test.cpp : 知识点练习和测试,用于单步调试,跟 ...

  8. spring 3配置文件中如何注入map list set等类型

    首先写个 javabean类吧,如下 package com.bean; import java.util.List; import java.util.Map; import java.util.P ...

  9. 普通用户登录Oracle DB Control

    使用 sys 或者 system 用户登录 Oracle DB Control 是没有问题的. 但是,如果是普通的用户需要登录Oracle DB Control,建表或者视图之类的, 则需要授权 SE ...

  10. FIFO算法

    package studyJava;class FIFO{ /* * 先进先出算法 * N:内存块的个数 * array:内存块 * size:页面数目 * */ private static fin ...