问题总结
来源:互联网 发布:淘宝订购服务怎么退款 编辑:程序博客网 时间:2024/06/02 03:37
1.C++类的构造函数和析构函数,可以写在外面:
#include <iostream>using namespace std;class String{ char *str; int Num[20]; int Count;public: String(char *s); ~String(); void Process(); void Show();};String::String(char *s) { if(s){ str= new char [strlen(s)+1]; str= strcpy(str,s); }else{ str = 0; } Count = 0;}String::~String() { delete [] str;}void String::Process() { int i=0,n; char *p= str; while (*p){ while(*p>='0'&&*p<'9'&&*p){ n=n*10+(*p-'a'); p++; } Num[i++]=n; } Count = i;}
2.C++的可以换行写,不影响编译:
#include <iostream>using namespace std;int main(){ int a[15]={1,2,3,4,5,6,7, 8,9,0,1,2,3 //这么写没毛病 ,4,5}; for (int i=0;i<15;i++){ cout<<a[i]<<endl; } //一句话的代码建议这么写,考试时候看起来方便,括号不至于太多 return 0;}
3.重载函数的类外实现
#include <iostream>using namespace std;class Complex{ float Real,Image;public: Complex(){Real=0;Image=0;} Complex(float r,float i){Real=r; Image=i;} void Print(){ cout<<Real; if(Image>0){cout<<"+";} if(Image!=0){cout<<Image<<"i";} cout<<endl; } Complex operator=(Complex c){ Real = c.Real; Image = c.Image; return *this; } Complex operator,(Complex c);};Complex Complex::operator,(Complex c){ Complex t; t.Real = Real; t.Image = Image; return t;}int main(){ Complex c1(3.4,5.6),c2(10.5,-12.3),c3; c1.Print(); c2.Print(); c3=(c1,c2); c3.Print(); return 0;}
4.字符串输出问题:
#include <iostream>using namespace std;int main(){ char s[]="Rep\0ch"; int m=sizeof(s); cout<<"m="<<m<<endl; cout<<"输出字符串:"<<endl; for (int i=0;i<sizeof(s);i++){ cout<<s[i]<<endl; } return 0;}
输出的结果是:
m=7
输出字符串:
R
e
p
♦️
c
h
♦️
Process finished with exit code 0
阅读全文
0 0
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 问题总结
- 《OC基础教程》读书笔记7-NSArray,NSMutableArray初识
- 一个模仿Airbnb APP的一键切换UI
- 彻底理解js中this的指向,不必硬背
- C++字符串操作详解
- 通俗的解释一下范式
- 问题总结
- ES6学习——class到底是个啥
- 常用算法——贪婪
- 以图搜视频的两篇短论文(Stanford)
- EL表达式
- vue.js监听滚动条加载更多数据
- 彻底弄懂css中单位px和em,rem的区别
- 关于lower_bound与upper_bound的实用技巧
- SpringBoot(1)--------helloworld