例2.1

使用成员函数的实例。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 struct Point
{
void Setxy(double a, double b)//成员函数,用来重新设置数据成员
{
x = a;
y = b;
} void Display()//成员函数,按指定格式输出数据成员的值
{
cout << x << "\t" << y << endl;
} double x, y;//数据成员
}; void main()
{
Point a;//定义对象a a.Setxy(10.6, 18.5);//设置对象a的数据成员
a.Display();//显示对象a的数据成员 cout << a.x << "\t" << a.y << endl;
}

例2.2

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 struct Point
{
private:
double x, y;//数据成员 public:
void Setxy(double a, double b)//成员函数,用来重新设置数据成员
{
x = a;
y = b;
} void Display()//成员函数,按指定格式输出数据成员的值
{
cout << x << "\t" << y << endl;
}
}; void main()
{
Point a; a.Setxy(10.6, 18.5); a.Display(); //cout << a.x; //1>------已启动生成: 项目: hello, 配置 : Debug Win32------
// 1> main.cpp
// 1>c:\users\denggl18.gdctc\documents\visual studio 2015\projects\hello\hello\main.cpp(33) : error C2248 : “Point::x” : 无法访问 private 成员(在“Point”类中声明)
// 1> c:\users\denggl18.gdctc\documents\visual studio 2015\projects\hello\hello\main.cpp(10) : note : 参见“Point::x”的声明
// 1> c:\users\denggl18.gdctc\documents\visual studio 2015\projects\hello\hello\main.cpp(8) : note : 参见“Point”的声明
// == == == == == 生成 : 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 == == == == == }

例2.3

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 struct Point
{
private:
double x, y;//数据成员 public:
Point()//无参数构造函数
{ }; Point(double a, double b)//具有两个参数的构造函数
{
x = a;
y = b;
} void Setxy(double a, double b)//成员函数,用来重新设置数据成员
{
x = a;
y = b;
} void Display()//成员函数,按指定格式输出数据成员的值
{
cout << x << "\t" << y << endl;
}
}; void main()
{
Point a;//定义对象a
Point b(18.5, 10.6);//定义对象b并赋初值 a.Setxy(10.6, 18.5);//设置变量a的数据成员 a.Display();//显示变量a的数据成员
b.Display();//显示变量b的数据成员
}

例2.4

定义类的实例。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point
{
private:
double x, y;//类Point的数据成员 public:
Point()//类Point的无参数构造函数
{ }; Point(double a, double b)//具有两个参数的构造函数
{
x = a;
y = b;
} void Setxy(double a, double b)//成员函数,用来重新设置数据成员
{
x = a;
y = b;
} void Display()//成员函数,按指定格式输出数据成员的值
{
cout << x << "\t" << y << endl;
}
}; void main()
{
Point a;//定义类Point的对象a
Point b(18.5, 10.6);//定义类Point的对象b并初始化 a.Setxy(10.6, 18.5);//为对象a的数据成员赋值 a.Display();//显示对象a的数据成员
b.Display();//显示对象b的数据成员
}

例2.8

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void main()
{
string str1("We are here!");
string str2 = "Where are you?"; cout << str1[] << str1[] << "," << str1 << endl;
cout << str2[] << str2[] << "," << str2 << endl;
cout << "please input word:"; cin >> str1; cout << "length of the " << str1 << " is " << str1.size() << "." << endl;
}

例2.9

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string> using namespace std; void main()
{
cout << "Enter the date in American format"
<< "(e.g., December 25, 2002):"; string Date; getline(cin, Date, '\n'); int i = Date.find(" "); string Month = Date.substr(, i); int k = Date.find(","); string Day = Date.substr(i + , k - i - );
string Year = Date.substr(k + , );
string NewDate = Day + " " + Month + " " + Year; cout << "Original date:" << Date << endl;
cout << "Converted date:" << NewDate << endl;
}

例2.10

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <complex>
#include <string> using namespace std; void main()
{
complex <int> num1(, );
complex <float> num2(3.5, 4.5); string str1("real is ");
string str2 = "image is "; cout << str1 << num1.real() << ',' << str2 << num1.imag() << endl;
cout << str1 << num2.real() << ',' << str2 << num2.imag() << endl;
}

例2.11

演示string对象的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string>
#include <algorithm>
#include <iterator> using namespace std; void main()
{
string str1 = "we are here!", str2 = str1; reverse(&str1[], &str1[] + );//str1字符串的元素逆向 cout << str1 << endl;//输出逆向后的内容 copy(&str1[], &str1[] + , &str2[]);//原样复制到str2 cout << str2 << endl;//输出str2 reverse_copy(&str2[], &str2[] + , ostream_iterator<char>(cout));//逆向输出str2
}

例2.12

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

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <iterator> using namespace std; void main()
{
string str1 = "wearehere!", str2(str1);//使用str1初始化 reverse(str1.begin(), str1.end());//字符串元素逆向 cout << str1 << endl; copy(str1.begin(), str1.end(), str2.begin());//原样复制到str2,str2应能容纳下str1 sort(str1.begin(), str1.end());//默认升幂排序 cout << str1 << endl;//输出排序结果
cout << str2 << endl;//输出字串str2的内容 reverse_copy(str1.begin(), str1.end(), str2.begin());//逆向复制到字串str2的内容 cout << str2 << endl;//输出逆向后的str2的内容 reverse(str2.begin() + , str2.begin() + );//字串str2部分逆向 copy(str2.begin() + , str2.begin() + , ostream_iterator<char>(cout));//输出逆向后的部分内容 cout << endl; sort(str1.begin(), str1.end(), greater<char>());//降幂排序 cout << str1 << endl;//输出排序后的字符str1 str1.swap(str2);//互相交换内容 cout << str1 << " " << str2 << endl;
}

例2.13

演示string对象数组的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>
#include <string>
#include <algorithm>
#include <iterator> using namespace std; void main()
{
string str[] = { "We are here!","Where are you?","welcome!" }; for (int i = ; i < ; i++)
{
copy(str[i].begin(), str[i].end(), ostream_iterator<char>(cout));
cout << endl;
} str[].swap(str[]);
str[].swap(str[]); for (int i = ; i < ; i++)
{
cout << str[i] << endl;
}
}

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. MYSQL create database 和 create table 做了一些什么!

    create database Studio; 这样就可以创建一个数据库了.他包涵一些什么呢? 可以看到它创建了一个文件夹,下面我们进去看一下它里面有一些什么东西. 还是先建一张表再进去吧,运行一下这 ...

  2. tomcat------https单向认证和双向认证

     一.https分为单向认证和双向认证: 单向认证就是说,只有客户端使用ssl时对服务器端的证书进行认证,也就是说,客户端在请求建立之前,服务器端会向客户端发送一个证书,一般情况下,这种证书都是由自己 ...

  3. QT:“下载速度柱状图”的模拟实现——思路真好,会动脑筋,连我都有了启发(这个思路好像是通用的)

    不知是哪个版本的迅雷,有个“下载速度柱状图”的小界面,我比较喜欢(只不过最新版本的迅雷却没了),所以决定来山寨一个.当然,这个山寨品不能下载文件,呵呵. 思路:1:将界面的背景涂成黑色2:每隔0.1秒 ...

  4. 静态查找_Search

    #include <stdio.h> #define MAXSIZE 50 #define OK 1 #define ERROR 0 int F[MAXSIZE];//斐波那契数列 int ...

  5. poj 1083 Moving Tables_dp

    题意:给你n个凳子,接着告诉你一个凳子从a房间到b房间,运输时间为10分钟,走廊很窄能通过一张凳子,当然不堵塞的话能同时扮凳子,问最小花费多少时间 因为数据很小就直接用数组统计了,a,b如果是奇数的话 ...

  6. xml中,button改变背景颜色方法

    在画几个设置界面,用到了button控件,对于button空间的背景色在不同状态下的颜色改变方法,做了一下尝试,发现了两种背景颜色改变的方法,就总结了下. 方法一尝试了好多遍才好,要点在于,在sele ...

  7. 【iOS开发-35】有了ARC内存管理机制,是否还须要操心内存溢出等问题?——面试必备

    答案:必需要操心啊,ARC也不是万能的. 这里主要是涉及到集合类的数据类型. 比方数组,我们定义了一个可变数组muarr1,然后把一个对象p1加到muarr1中,此时会对这个对象retain一次,相当 ...

  8. Nginx学习——http配置项解析编程

    http配置项解析编程 配置config ngx_addon_name=ngx_http_mytest_module HTTP_MODULES="$HTTP_MODULES ngx_http ...

  9. 移动端H5页面之iphone6的适配

    iphone6 及 iphone 6 plus 已经出来一段时间了.很多移动端网站,以前写死body 为320px的,现在估计也忙着做适配了. 大屏幕手机其实一直有,只是以前大家没怎么重视,移动端的H ...

  10. 用于Lucene的各中文分词比较

    对几种中文分析器,从分词准确性和效率两方面进行比较.分析器依次为:StandardAnalyzer.ChineseAnalyzer.CJKAnalyzer.IK_CAnalyzer.MIK_CAnal ...