类型转换 IO

define _CRT_SECURE_NO_WARNINGS

include

include

include

//标准命名空间(包含很多标准的定义)
//standard
using namespace std;
//C++ 类型转换
//static_cast 基本类型转换 普通
//const_cast; 去常量转换
//dynamic_cast 子类类型转为父类类型
//reinterpreet_cast 不具備移植行
//原始类型转换 所有情况都是一种 可读性不高,有可能潜在的风险

//常量转换
//void* func(int type){
// switch (type)
// {
// case 1:{
// cout << type << endl;
// int i = 0;
// return &i;
// break;
// }
// case 2:{
// cout << type << endl;
// char i = 'a';
// return &i;
// break;
// }
// default:
// return NULL;
// break;
// }
// int i = 0;
// return &i;
//}
//void func2(char* cp){
// cout << cp << endl;
//}
//
//void main(){
// //自动类型转换
// /
double d = 9.5;
// int i = d;/
///

// int i=8;
// double j = 9.4;
// i = static_cast(j);/
//
// int i = 9;
// int i_p = &i;
//
// //void
-> char
指针的转换
// //char* a=(char)func(1);
// //cout << a << endl;
//
// //C++
// //char
c)P=(char
)func(2);
// //char* c_p = static_cast> (func(2));
//
//
// //C++
// //func2(static_cast> (func(2))); 意图明显
// //func2((char*)func(2)); 意图不明显
//
//
//
// cout << i << endl;
// system("pause");
//}

//去常量转换
//void func(const char c[]){
// //c[1] = 'q';
// //char c1[] = (char[])c;
// //通过指针间接赋值 代码阅读性差 去掉常量的性质
// /char c_p = (char)c;
// c_p[1] = 's';
// cout << c << endl;
/
// char* sd=const_cast>(c);
// sd[1] = 'T';
// cout << c << endl;
//}
//void main(){
// char c[] = "werd";
// func(c);
// system("pause");
//}
//class Person{
//public:
// virtual void print(){
// cout << "人" << endl;
// }
//};
//class Man : public Person{
//public:
// virtual void print(){
// cout << "男人" << endl;
// }
// void working(){
// cout << "工作" << endl;
// }
//};
//class Women:public Person{
//public:
// virtual void print(){
// cout << "女人" << endl;
// }
// void working(){
// cout << "工作" << endl;
// }
//};
//void func(Person obj){
// obj->print();
// //转成实际类型 并不知道转型失败
// //Man* m = (Man)obj;
// //m->working();
//
// //轉型失败返回NULL 根据返回值 是否为空 进行方法调用
// Women
m = dynamic_cast(obj);
// m->print();
// m->working();
//}

//void func1(){
// cout << "func1" << endl;
//}

//char* func2(){
// cout << "func2" << endl;
// return "abc";
//}

//typedef void(*f_p)();

//void main(){
// //函數指針的數組
// f_p k[5];
// k[0] = func1;
// //C 轉換
// //k[1] =(f_p)(func2); 數組中函數指針的轉換
// k[1] = reinterpret_cast(func2);

// system("pause");
//}

//I/O流文件的操作
//#include
//void main(){
// char* fname = "D://dest.tet";
// //輸出流
// ofstream fout(fname);
// if (fout.bad()){
// return;
// }
// fout << "jack" << endl;
// fout << "rose" << endl;
//
// //關閉
// fout.close();
//
// //讀取
// ifstream fin(fname);
// if (fin.bad()){
// return;
// }
// char ch;
// while (fin.get(ch))
// {
// cout << ch;
// }
// fin.close();
// system("pause");
//}

//二进制文件
/*
void main(){
char* src = "c://src.jpg";
char* dest = "c://dest.jpg";

//输入流
ifstream fin(src, ios::binary);
//输出流
ofstream fout(dest, ios::binary);

if (fin.bad() || fout.bad()){
return;
}

while (!fin.eof()){
//读取
char buff[1024] = {0};
fin.read(buff,1024);

//写入
fout.write(buff,1024);
}

//关闭
fin.close();
fout.close();

system("pause");
}
*/

/*
//C++对象的持久化
class Person
{
public:
Person()
{

}
Person(char * name, int age)
{
this->name = name;
this->age = age;
}
void print()
{
cout << name << "," << age << endl;
}
private:
char * name;
int age;
};

void main()
{
Person p1("柳岩", 22);
Person p2("rose", 18);
//输出流
ofstream fout("c://c_obj.data", ios::binary);
fout.write((char)(&p1), sizeof(Person)); //指针能够读取到正确的数据,读取内存区的长度
fout.write((char
)(&p2), sizeof(Person));
fout.close();

//输入流
ifstream fin("c://c_obj.data", ios::binary);
Person tmp;
fin.read((char*)(&tmp), sizeof(Person));
tmp.print();

fin.read((char*)(&tmp), sizeof(Person));
tmp.print();

system("pause");

}
*/

//stl standard template library 标准模板库
//util
//c++ 集合-> java 集合

include

/*
void main()
{
string s1 = "craig david";
string s2(" 7 days");
string s3 = s1 + s2;

cout << s3 << endl;

//转c字符串
const char* c_str = s3.c_str();
cout << c_str << endl;

//s1.at(2);

system("pause");
}
*/
//
////容器
//#include
//
//void main()
//{
// //动态数组
// //不需要使用动态内存分配,就可以使用动态数组
// vector v;
// v.push_back(12);
// v.push_back(10);
// v.push_back(5);
//
// for (int i = 0; i < v.size(); i++)
// {
// cout << v[i] << endl;
// }
//
// system("pause");
//}

你可能感兴趣的:(类型转换 IO)