C++程序实例

实例1

#include<iostream>

#include<iomanip>//进制流输出头文件 
#include<string>




using namespace std;




//using std::endl;
//using std::cout;
//using std::cin;
int add(int a, int b)
{
return a+b;
}
int main()
{
cout<<"hello world!"<<endl;
cout<<30<<endl;
cout<<20.8<<endl;
int i = 10;
cout<< i <<endl;
cout<<i*8+3<<endl;
cout<<add(20,30)<<endl;//endl换行符
cout<<"hello"<<" "<<"world!"<<endl<<flush;//用流输出符链接  flush  刷新缓冲区 
cout<<setfill('*')<<setw(10)<<2<<3<<endl;//setw设置位宽  如果设置的宽度小于数据的实际宽度 setw就不起作用了
cout<<hex<<16<<endl;
cout<<oct<<16<<endl;//最后一个输出进制 如果以后不改输出类型 就还按照8进制输出 
cout<<12<<endl;
int num1;
float num2;
string str;
// cin>>num1>>num2>>str;
// cout<<num1<<endl;
// cout<<num2<<endl;
// cout<<str<<endl;
while(cin>>str){
if(str=="exit")
break;
cout<<str<<endl;

}


}


实例2

#include<iostream>
#include<string>
using namespace std;


int main(){
string str1;//空字符串 
string str2("str2");
string str3="str3";
string str4=("str4");
string str5(10,'a');
string str6=str5;
str1="hello world!";
str2="hello";
str1+=str2;
str2=str3+"world!";
// str2="hello"+"world";//错的 必须有一个是str型


str2==str3;//比较 返回布尔类型 
str2!=str3;
str2>=str3;//用字典顺序比较

int nsize=str2.size();//查看字符串长度
//str2[n];//n的范围是0到n-1;
str2.empty();

return 0;

 
}

实例3

#include<iostream>
#include<vector>
#include<string>
using namespace std;


int main()
{
//初始化vector 
vecotr<int> v1;//定义一个v1空容器
vector<int> v2(v1);
vector<int> v3=v1; //v3shiv1de fuben
vector<int> v4(10,100);
vector<int> v5(100);
vector<int> v6;
/向vector添加元素//
v6.push_back("hello world!");
v5.push_back(50);
///如何从vector删除元素///
v4.pop_back();
int nNum=v4[2];
v4.at(2);//访问下标元素符位置  范围 0到size()-1
v4.front();//返回最前面的元素
v4.back();//返回最后面的元素
v4.size();//判断大小
v4.empty();//判断是否为空

还可以做一些判断
v1==v2;
v1>=v2;
 
 

return 0;
}

实例4

文本编辑器

vetor 版本

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string str;//接收键盘每一行的字符
vector<string> v;//保存所有的字符
while(getline(cin,str))
{
if(str=="q")
break;
v.push_back(str);


int nChars = 0;
for(int i=0;i<v.size();i++)
{
string s = v[i];
nChars += s.size();

}
cout<<"total lines:"<<v.size()<<" "<<"toatl chars:"<<nChars<<endl;
for(int i=0;i<v.size();i++)
{
string s = v[i];
cout<<i<<" "<<s<<" "<<s.size()<<endl;


}

return 0;
}
 

实例6

#include<iostream>
using namespace std;


typedef struct{
int a;
int b[1000];

}BigStruct;


void swap(int& a,int& b)
{
int temp = a;
a = b;
b = temp;
}
void func(const BigStruct a)
{

///
}


const BigStruct& func()
{


}
int main()
{
int a = 0;
int &b = a;//b是a的别名与引用 注意b只能是a的引用 只要初始化以后 

return 0;

}


实例7

#include<iostream>
using namespace std;


void move(int step = 1,int delta = 20);


int main()
{


move(2,40);
move(2);
move();// 不写的使用默认值    注意点 1 哪个开始有默认值 后面都得有 2 声明时有初始值  定义时就不要有初始值 
return 0;

}

实例8

#include<iostream>
using namespace std;
//函数的重载 如果参数列别也相同 只是返回值不一样的话 这个不是函数重载 编译器会报错 


int add(int a,int b)
{

return a+b;
}
float add(float a,float b)
{

return a+b;
}
int main()
{
int a =10;
int b = 20;
float a = 20.2;
float b = 20.8;


return 0;
}


实例9

#include<iostream>
using namespace std;


template<typename T>
T add(T a,t b)
{
return a+b;
}
//函数模板 分解成下面函数
int add(int a, int b)
{
return a+b;

int main()
{
add(10,20)
float f1 = 10.5;
float f2 = 20.5;
add(f1,f2);
return 0;
}

实例10

面向对象概念

对象:世界上任何一个事物都可以说是一个对象

封装与信息隐藏:就像汽车 把内部制造细节隐藏 留出来 方向盘 简单的东西 有利于人们操作

抽象:抽象 就是对具体事物的 抽象集合 例如人 汽车

继承与重用:就是每一个新类 例如汽车 都是在以前基础上发展过来的

多态性:对一个命令有不同的反应 每一个具体对象有不同反应

#include<iostream>
using namespace std;


class Student{
public:
void print()
{
std::cout<<name<<","<<age<<","<<addr<<std::endl;


}
int getAge();
private:
char name[128];
int age;
char addr[128];

};
inline int Student::getAge()
{
return 0;
}
int main()
{
Student stu;
stu.print();
stu.getAge();
return 0;
}


实例11

#include<iostream>
#include"Time.h"


using namespace std;


int main()
{

CTime time;
time.setHour(10);
time.setMinute(20);
time.setSecond(50);

cout<<time.getHour()<<":"<<time.getMinute()<<":"<<time.getSecond()<<endl;
return 0;
}


#ifndef TIME_H
#define TIME_H


class CTime{
public:
void setHour(int Hour);
void setMinute(int Minute);
void setSecond(int Second);

int getHour();
int getMinute();
int getSecond();






private:
int m_nHour;
int m_nMinute;
int m_nSecond;
};
#endif


#include"Time.h"


void CTime::setHour(int hour)
{
m_nHour = hour;
}


void CTime::setMinute(int minute)
{
m_nMinute = minute;
}


void CTime::setSecond(int second)
{
m_nSecond = second;
}


int CTime::getHour()
{
return m_nHour;
}


int CTime::getMinute()
{
return m_nMinute;
}


int CTime::getSecond()
{
return m_nSecond;
}



实例10

cout<<time.getHour()//time的起始地址 赋给getHour()的内部的this指针


简单来说,这个东西只能在类的成员函数中用, 用法是我的XX,XX表示变量或者函数
this->a
this->test();
this就是指代类对象本身, 可以不写
当参数与成员变量同名时,可以用this来区分
this->a = a;
静态成员函数没有this
复制构造函数 也是以类的名称 作为函数名额 也没有返回值的
构造复制构造函数参数是用传引用的方式传递的
 
静态成员
静态数据成员
静态数据成员 为各个成员所共有的 都可以对这个成员进行访问和修改
静态数据成员是属于这个类 定义静态数据成员时 系统就会给他分配存储空间
 
const 对象
const 类名 对象名
类名 const 对象名
如果 一个对象被定义为 const对象 就不能访问非const对象里成员
 
友元
友元 函数可以修改类中是有部分成员函数
 
 
 

运算符重载

对运算符赋予新的含义


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值