2020-12-20
一、
#include<iostream>
using namespace std;
int main()
{
char str[100],str1[100];
cin.read(str,5);//输入abcdefg\n
str[5]='\0';
cout<<str<<endl;//输出abcde
cin.read(str1,5);//输入hijklmn\n
str1[5]='\0';
cout<<str1<<endl;//输出fg\nhi
return 0;
}
//说明iostream::read()是会保存上一次读取过量的输入值的
二、
#include<iostream>
using namespace std;
class ONE
{
int c;
public:
ONE():c(0){cout<<1;}
ONE(int n):c(n){cout<<2;}
};
class TWO
{
ONE one1;
ONE one2;
public:
TWO(int m):one2(m)//相当于TWO(int m):one1(),one2(m),one1()的括号不可以删
{
cout<<3;
}
};
int main()
{
TWO t(4);
return 0;
}
//结果是123,要注意没有显式表现出来的ome1的构造
三、
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char ch;
cin.get(ch);//或者ch=cin.get();
cout<<int(ch)<<endl;
return 0;
}
//结果是97
四、
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char str[100]="abcdefg";
//ostream&cout.write(char *str,int length)
//函数的返回类型为类的对象引用,可以串联使用
cout.write(str,3).write(str+2,4);
return 0;
}
//abccdef
五、
cout<<(x=3+1);//不可以去括号
str[2,3];//其实就是str[3],里面是逗号表达式
六、
#include<iostream>
using namespace std;
const int N=100;
class CStack
{
int top;
int stack[N];
public:
CStack()
{
top=0;
cout<<"Hello!";
}
~CStack()
{
cout<<"Bye";
}
void push(int i);
int pop();
};
void CStack::push(int i)
{
if(top==N)
{
cout<<"Overflow";
return;
}
else
{
top++;
stack[top]=i;
}
}
int CStack::pop()
{
int temp;
if(top==0)
{
cout<<"Underflow";
return 0;
}
else
{
temp=stack[top];
top--;
return temp;
}
}
int main()
{
CStack *ptr=new CStack;//Hello!
ptr->push(10);
ptr->push(50);
cout<<ptr->pop()<<" ";//50
cout<<ptr->pop()<<" ";//10
cout<<ptr->pop()<<" ";//Underflow0
cout<<"OK!"<<endl;//OK!\n
delete ptr;//Bye
return 0;
}
//Hello!50 10 Underflow0 OK!
//Bye