一、C++中的结构与类
1、函数与数据共存
在C++中定义一个结构
struct 成员名{
数据成员;
成员函数;
};
#include <iostream>
using namespace std;
struct Point{
private: //私有属性 使用private关键字定义数据成员,产生封装性
double x,y;
public: //公有方法
void Setxy(double a,double b){
x=a;
y=b;
}
void Display(){
cout<<x<<"\t"<<y<<endl;
}
};
int main() {
Point a;
a.Setxy(12.3, 25.6);//调用方法 设置参数
a.Display();
//cout<<x<<"\t"<<y<<endl;
return 0;
}
2.使用构造器初始化结构对象
#include <iostream>
using namespace std;
struct Point{
private: //私有属性 使用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;
}
};
int main() {
Point a;
Point b(23.6,3);//使用含参构造器 构造函数名 对象名(初始化参数);
a.Setxy(12.3, 25.6);//调用方法 设置参数
a.Display();
b.Display();
//cout<<x<<"\t"<<y<<endl;
return 0;
}
3.从结构到类
#include <iostream>
using namespace std;
class Point{
private: //私有属性 使用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;
}
};
int main() {
Point a;
Point b(23.6,3);//使用含参构造器 构造函数名 对象名(初始化参数);
a.Setxy(12.3, 25.6);//调用方法 设置参数
a.Display();
b.Display();
//cout<<x<<"\t"<<y<<endl;
return 0;
}
将struct替换为class 就是一个标准类
类名Point |
具有的属性x和y |
提供的操作 构造函数Point Setxy用来给对象赋值 Display用来输出x和y |
二、面向过程与面向对象的区别
http://blog.csdn.net/gogokongyin/article/details/51111528
三、C++面向对象程序设计的特点
抽象、封装、继承、多态
四、string类和complex类
string类
string 类名 |
str 属性 |
string 构造函数 find 在str字符中检索所需要的字串 size 计算并输出str存储的单词长度 substr 返回str字符串中的字串 |
使用string类前要先加载这个类的头文件
#include<string>
#include <iostream>
#include<string>
using namespace std;
int main() {
string str1("we are the world!we are the world!");
string str2="hello world!";//两种初始化方式
// cout<<str1;
// for(int i=0;i<str1.length();i++){ //使用length属性
// cout<<str1[i];
// }
// cout<<"just put a word...";
// cin>>str1;
// cout<<"the length of the "<<str1<<" is "<<str1.size()<<endl;//调用size()方法
// string newstr=str1.substr(3,3);//(计数从0开始)从第三个字符开始,截取三个字符
// cout<<newstr;
// int i=str1.find("are", 0);//传入两个参数,第一个是查找的字符,第二个是开始查找的起点,若不给起点,默认为0
// cout<<i; //查找到一个就会停止,并返回第一个字符所在位子,若没找到,返回-1
string inpstr;
cout<<"just input sth...";//输入all right
//cin>>inpstr; //读取到空格 输出all
getline(cin,inpstr,'\n');//读取整行输入内容 输出 all right
cout<<inpstr;
return 0;
}
string例子,将美国格式日期转换为国际格式
思想:使用find检索,再使用substr截取,然后赋值、重组
#include <iostream>
#include <string>
using namespace std;
int main() {
string year,month,day,data1;
cout<<"input a data like"<<" May 28,2018"<<endl;
getline(cin,data1,'\n');
int i=data1.find(" ",0);
month=data1.substr(0, i);
int j=data1.find(",", 0);
day=data1.substr(i+1,j-i-1);
year=data1.substr(j+1, data1.size()-j);
string data2=year+" "+month+" "+day;
cout<<"you input data is "<<data1<<endl;
cout<<"new data is "<<data2<<endl;
return 0;
}
complex类
complex类定义复数对象,在程序中包含头文件include<complex>。
复数(complex number)类需要两个初始值:实部(real)和虚部(imag)。
complex <数据类型>对象名(实部值,虚部值);
实部和虚部的数据类型可以为整数(int),也可以为实数(float);
complex后跟的数据类型只是说明,并不限制实部和虚部,但对输出结果有影响。
#include <iostream>
#include<complex>
using namespace std;
int main() {
complex <int> num1(2.6,4.2);
complex <float>num2(3,4);
cout<<num1<<" real:"<<num1.real()<<" imag:"<<num1.imag()<<endl;
cout<<num2<<" real:"<<num2.real()<<" imag:"<<num2.imag()<<endl;
return 0;
}
输出结果:
(2,4) real:2 imag:4
(3,4) real:3 imag:4