下午多益笔试 记得一点点题目回来敲了一下 下次上下一部分
#include <iostream>
using namespace std;
class A
{
public:
virtual void f1()
{
cout<<"A::f1()"<<endl;
f2();
}
virtual void f2()
{
cout<<"A::f2()"<<endl;
}
};
class B :public A
{
public:
virtual void f1()
{
cout<<"B::f1()"<<endl;
f2();
}
virtual void f2()
{
cout<<"B::f2()"<<endl;
}
};
class C:public B
{
public:
virtual void f1()
{
cout<<"C::f1()"<<endl;
}
};
void testClass()
{
A *AA;
C cc;
AA=&cc;
AA->f1();
AA->f2();
}
考继承时候对象的函数是哪个
#include <iostream>
using namespace std;
class String
{
public:
String(const char* m_str =NULL);//默认构造函数
String(const String &str);//拷贝构造函数
~String();//析构函数
void operator+(const String &str);//+运算符
void operator=(const String &str);//赋值运算符
private:
char* m_data;
};
String::String(const char* m_str)
{
cout<<"default constructor called!"<<endl;
int len=0;
while(*(m_str+len)!='\0')
len++;
m_data =new char(len+1);
m_data[len]='\0';
int i;
for(i=0;i<len;i++)
{
m_data[i]=m_str[i];
}
//cout<<"length ="<<len<<" string="<<this->m_data<<endl;
}
String::~String()
{
cout<<"~String() called!"<<endl;
}
String::String(const String &str)
{
cout<<"copy constructor called!"<<endl;
int len=0;
while(*(str.m_data+len)!='\0')
len++;
m_data =new char(len+1);
m_data[len]='\0';
int i;
for(i=0;i<len;i++)
{
m_data[i]=str.m_data[i];
}
}
void String::operator+(const String &str)
{
int len1=0;
int len2=0;
while(*(this->m_data+len1)!='\0')
len1++;
while(*(str.m_data+len2)!='\0')
{
len2++;
}
char* temp= new char(len1+len2);
temp[len1+len2-1]='\0';
int i;
for(i=0;i<len1;i++)
{
temp[i]=this->m_data[i];
}
for(i=0;i<len2;i++)
{
temp[i+len1-1]=str.m_data[i];
}
cout<<temp<<endl;
}
void String::operator=(const String &str)
{
int len=0;
while(*(str.m_data+len)!='\0')
len++;
char* temp =new char(len+1);
temp[len]='\0';
int i;
for(i=0;i<len;i++)
{
temp[i]=str.m_data[i];
}
cout<<this->m_data<<endl;
}
试卷是写了类内的几个函数,让我们实现,主要是const不能直接拷贝,巨坑。重载运算符也不熟悉。
#include <iostream>
#include <stdio.h>
using namespace std;
void test1()
{
int a=0210;
printf("%x\n",a);
}
void test2()
{
char str[] ="abcdefghijklnm";
printf("%s\n",str);
printf("%s\n",str+3);
printf("%s\n",(char*)((int*)str+1));
}
char* tenToBinary(int value)
{
int i=1;
int temp=1;
while(temp<value)
{
temp=temp*2;
i++;
}
char *str=new char(i);
str[i-1]='\0';
temp=value;
i=i-2;
while(temp>=1)
{
if(temp%2==1)
str[i]='1';
else if(temp%2==0)
str[i]='0';
i--;
temp=temp/2;
}
return str;
}
int 零开头的是八进制 %x是十六进制
(char*)((int*)str+1)的意思就是一个整数4个byte就往右移4个位置 %s输出字符串
十进制转二进制不记得了 巨坑。