//題目1
//int a[3];
//a[0]=0; a[1]=1; a[2]=2;
//int *p, *q;
//p=a;
//q=&a[2];
//a[q-p]的值是多少?為什麼?
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
int a[3];
a[0]=0;
a[1]=1;
a[2]=2;
int *p,*q;
p=a;
q=&a[2];
cout<<a[q-p]<<endl;
cout<<q<<endl;
cout<<p<<endl;
cout<<q-p<<endl; //相差八個位元組,因為int類型是4個位元組,是以下标為2,是以a[2]=2
return 0;
}
//題目2
//const 有什麼用途?請用附上代碼例子佐證!
//const作用
1.可以定義const常量,具有不可變性
例如:const int MAX=100;int Array[MAX];
2.便于編譯器進行類型檢查,使編譯器對處理内容有更多的了解,消除一些隐患。例如:void f(const int i){} 編譯器就知道i是一個常量,不允許修改;
3.可以避免意義模糊的數字出現,同樣可以很友善的進行參數的調整和修改。同宏定義一樣,可以做到不變則以,一變都變!在(1)中,如果想修改MAX的内容,隻需要修改:const int MAX=you want即可;
4.可以保護被修飾的東西,防止意外的修改,增強程式的健壯性。還是上面的例子,如果在函數體内修改了i,編譯器就會報錯,例如:
void f(const int i){i=10;//error}
5.為重載函數提供了一個參考
class A{
void f(int i){...}//一個函數
void f(int i)const{...}//上一個函數的重載
};
6.可以節省空間,避免不必要的記憶體配置設定。例如
#define PI 3.14159 //常量宏
const double Pi=3.14159;//此時并未将Pi放入ROM中。。。
double i=Pi;//此時為Pi配置設定記憶體,以後不再配置設定!
double i=PI;//編譯起見進行宏替換,配置設定記憶體
double j=Pi;//沒有記憶體配置設定
double J=Pi;//再次進行宏替換,又一次配置設定記憶體!
const定義常量從彙編的角度來看,隻是給出了對應的記憶體位址,而不是像#define一樣給出的是立即數,是以,const定義的常量在程式運作過程中隻有一份拷貝,#define定義的常量在記憶體中又若幹份拷貝
7.提高了效率,編譯器通常不為普通的const常量配置設定記憶體空間,而是将他們儲存在符号表中,這使得它成為一個編譯期間的常量,沒有了存儲與讀取記憶體的操作,是的它的效率更高。
//題目3:
//引用跟指針的差別?請用附上代碼例子佐證!
//1.從記憶體上來講,系統為指針配置設定記憶體空間,而引用與綁定的對象共享記憶體空間,系統不為引用變量配置設定記憶體空間
//2.指針初始化後可以改變指向的對象,而引用定義的時候必須要初始化,而且初始化後不允許再重新綁定對象
//3.是以引用通路對象是直接通路的。指針通路對象是間接通路的。
//4.如果pa是指針,那麼*pa就是引用了。但是兩者在作為形參的時候非常相似,差別是指針拷貝副本,引用不拷貝。
void pt(int *pta,int *ptb)
int *ptc;
ptc=pta;
pta=ptb;
ptb=ptc;
void ref(int &ra,int &rb)
int rc;
rc=ra;
ra=rb;
rb=rc;
int a=3;
int b=4;
int *pa=&a;
int *pb=&b;
pt(pa,pb);
cout<<a<<' '<<b<<endl;
ref(a,b);
//題目4:
//學生類:學生的姓名成績及相關的運算,添加英語成績,并根據總成績對學生姓名進行排序
class Student
private:
char name[20];
float ChineseScore,MathScore,EnglishScore,Total;
public:
void student(char *Name,float chinese,float math);
void addEnglishScore();
void countTotalScore(double &total);
void getScores(char **name,double &chinese,double &math,double &english);
void showStudent(char *name,double ch,double ma,double en,double total);
float total();
char* Name();
float ch();
float ma();
float en();
void setTotal(float p);
};
void Student:: setTotal(float p)
Total=p;
char * Student::Name(){
char *p=name;
return p;
float Student::ch()
return ChineseScore;
float Student:: ma()
return MathScore;
float Student::en()
return EnglishScore;
void Student::student(char *Name,float chinese,float math)
int i=0;
while (*Name!='\0') {
name[i]=*Name;
Name++;
i++;
}
name[i] = '\0'; //加上\0結束,防止輸出的name有亂碼
ChineseScore = chinese;
MathScore = math;
void Student::addEnglishScore()
cout<<"請添加英語成績"<<endl;
cin>>EnglishScore;
void Student::countTotalScore(double &total)
total = ChineseScore + MathScore + EnglishScore;
void Student::getScores(char **Name,double &chinese, double &math, double &english)
*Name=p;
chinese=ChineseScore;
math=MathScore;
english=EnglishScore;
float Student::total()
return Total;
void Student::showStudent(char *name,double ch,double ma,double en,double total)
cout<<"姓名:"<<name<<" "<<"國文:"<<ch<<" "<<"數學:"<<ma<<" "<<"英語:"<<en<<" "<<"總分:"<<total<<endl;
float *funB(float *pNum,int N)
if (N>0) {
for (int i=0; i<N-1; i++) {
for (int j=0; j<N-1-i; j++) {
if(pNum[j]>pNum[j+1])
{
float temp;
temp=pNum[j];
pNum[j]=pNum[j+1];
pNum[j+1]=temp;
}
}
}
float* p=pNum;
cout<<"請輸入N:"<<endl;
int N;
cin>>N;
Student stu[N];
float a[N];
do {
//Student stu[i];
stu[i].student("dingxiaowei", 90, 91);
stu[i].addEnglishScore();
char *Name;
double ch,ma,en,total;
stu[i].getScores(&Name,ch, ma, en);
stu[i].countTotalScore(total);
stu[i].showStudent(Name, ch, ma, en,total);
a[i]=total;
stu[i].setTotal(total);
//cout<<a[i]<<endl;
} while (i<N);
// 列印存儲的資料
cout<<"\n"<<"total的成績:\n";
for (int i=0; i<N; i++) {
printf("%.2f \n",stu[i].total());
//排序後的順序
cout<<"\n按照成績排序後的學生:"<<endl;
float *p = funB(a, N);
for (int j=0; j<N; j++) {
for (int i=0; i<N; i++) {
if (p[j]==stu[i].total()) {
stu[i].showStudent(stu[i].Name(), stu[i].ch(), stu[i].ma(), stu[i].en(),stu[i].total());
break;
// Student stu1;
// stu1.student("dingxiaowei", 90, 91);
// stu1.addEnglishScore();
// char *Name1;
// double ch1,ma1,en1,total1;
// stu1.getScores(&Name1,ch1, ma1, en1);
// stu1.countTotalScore(total1);
// stu1.showStudent(Name1, ch1, ma1, en1,total1);
//
//
// Student stu2;
// stu2.student("wangnin", 90, 92);
// stu2.addEnglishScore();
// char *Name2;
// double ch2,ma2,en2,total2;
// stu2.getScores(&Name2,ch2, ma2, en2);
// stu2.countTotalScore(total2);
// stu2.showStudent(Name2, ch2, ma2, en2,total2);
// if (total1>=total2) {
// cout<<"成績由高到低排序"<<endl;
// stu1.showStudent(Name1, ch1, ma1, en1, total1);
// stu2.showStudent(Name2, ch2, ma2, en2, total2);
// }
// else{
本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366448,如需轉載請自行聯系原作者