| 小鱼儿 的个人资料蓝色妖姬照片日志列表 | 帮助 |
|
蓝色妖姬weiluyu2000 7月27日 上班五周了时间可真是快,上班已经五周了,今天又到周末了,总结一下也就完成几个程序,每天都在进步着,接到任务时就紧张而忙碌着,没任务的时候就学习而充实着,慢慢来吧,学东西也是急不来的!
周末邀请张霞来家吃火锅,^_^ 几个c++小程序 题1.分析以下程序的执行结果 #include<iostream.h> void swap(int &x,int &y) { int temp; temp=x; x=y; y=temp; } void main() { int x=10,y=20; swap(x,y); cout<<"x="<<x<<",y="<<y<<endl; } 解: 这里的函数采用引用调用的方式,所以输出为:x=20,y=10 注意:在函数调用里,引用调用与传址调用的效果相同,但更加简洁直观。 ---------------------------------------------------------- 题2.分析以下程序的执行结果 #include<iostream.h> void main() { int a[]={10,20,30,40},*pa=a; int *&pb=pa; pb++; cout<<*pa<<endl; } 解: pa为数组的指针,首先指向a[0],pb是pa的引用,当执行pb++时,也使pa指向了a[1],所以输出为:20 ------------------------------------------------------------- 题3.分析以下程序的执行结果 #include<iostream.h> class Sample { int x; public: Sample(){}; Sample(int a){x=a;} Sample(Sample &a){x=a.x++ +10;} void disp(){cout<<"x="<<x<<endl;} }; void main() { Sample s1(2),s2(s1); s1.disp(); s2.disp(); } 解: Sample类的Sample(Sample &a)构造函数是一个拷贝构造函数,将a对象的x增1然后加上10后赋给当前对象的x,由于a是引用对象,所以输出为: x=3 // ++运算的结果 x=12 // 2+10 -------------------------------------------------------------- 题4.分析以下程序的执行结果 #include<iostream.h> class Sample { int x,y; public: Sample(){x=y=0;} Sample(int i,int j){x=i;y=j;} void copy(Sample &s); void setxy(int i,int j){x=i;y=j;} void print(){cout<<"x="<<x<<",y="<<y<<endl;} }; void Sample::copy(Sample &s) { x=s.x;y=s.y; } void func(Sample s1,Sample &s2) { s1.setxy(10,20); s2.setxy(30,40); } void main() { Sample p(1,2),q; q.copy(p); func(p,q); p.print(); q.print(); } 解: 本题说明对象引用作为函数参数的作用。Sample类中的copy()成员函数进行对象拷贝。在main()中先建立对象p和q,p与q对象的x,y值相同,调用func()函数,由于第2个参数为引用类型,故实参发生改变;而第1个参数不是引用类型,实参不发生改变。所以输出为: x=1,y=2 x=30,y=40 ------------------------------------------------------- 题5.设计一个Book类,包含图书的书名、作者、月销售量等数据成员,其中书名和作者采用字符型指针,另有两个构造函数、一个析构函数和两个成员函数setbook()和print(),其中setbook()用于设置数据,print()用于输出数据,其说明如下: void print(ostream& output) 即引用输出流。 解: 依题意,本题程序如下: #include<iostream.h> #include<string.h> class Book { char *title; // 书名 char *author; // 作者 int numsold; // 月销售量 public: Book(){} Book(const char *str1,const char *str2,const int num) { int len=strlen(str1); title=new char[len+1]; strcpy(title,str1); len=strlen(str2); author=new char[len+1]; strcpy(author,str2); numsold=num; } void setbook(const char *str1,const char *str2,const int num) { int len=strlen(str1); title=new char[len+1]; strcpy(title,str1); len=strlen(str2); author=new char[len+1]; strcpy(author,str2); numsold=num; } ~Book() { delete title; delete author; } void print(ostream& output) // 输出流引用作为参数 { output<<"输出数据"<<endl; output<<" 书名:"<<title<<endl; output<<" 作者:"<<author<<endl; output<<" 月销售量:"<<numsold<<endl; } }; void main() { Book obj1("C语言程序设计","谭浩强",800),obj2; obj1.print(cout); obj2.setbook("C++语言程序设计","李春葆",300); obj2.print(cout); } 本程序的执行结果如下: 输出数据 书名:C语言程序设计 作者:谭浩强 月销售量:800 输出数据 书名:C++语言程序设计 作者:李春葆 月销售量:300 #include<iostream.h> class Cat { public: Cat(); Cat(const Cat &); ~Cat(); int getage()const{return *itsage;} void setage(int age){*itsage=age;} protected: int *itsage; }; Cat::Cat() { itsage=new int; *itsage=5; } Cat::~Cat() { delete itsage; itsage=0; } void main() { Cat frisky; cout<<"frisky's age:"<<frisky.getage()<<endl; cout<<"setting frisky to 6...\n"; frisky.setage(6); cout<<"creating boots from frisky\n"; Cat boots(frisky); cout<<"frisky's age:"<<frisky.getage()<<endl; cout<<"boots'age:"<<boots.getage()<<endl; cout<<"setting frisky to 7...\n"; frisky.setage(7); cout<<"frisky's age:"<<frisky.getage()<<endl; cout<<"boots'age:"<<boots.getage()<<endl; } 当添加了拷贝构造函数后,程序的运行结果为: frisky's age:5 setting frisky to 6... creating boots from frisky frisky's age:6 boots'age:6 setting frisky to 7... frisky's age:7 boots'age:6 解: 添加的拷贝构造函数如下: Cat::Cat(const Cat& c) { itsage=new int; *itsage=*c.itsage; } ----------------------------------------------- 题7.设计一个类Sample,有一个私有数据成员,建立该类的四个对象s1(n=10)、s2(n=20)、s3(n=30)、和s4(n=40),建立一个成员函数实现这些对象n值的累加。 解: 依题意,建立一个成员函数add(),其参数为Sample对象引用,用于累加对象n值。 程序如下: #include<iostream.h> class Sample { int n; public: Sample(){} Sample (int i){n=i;} void add(Sample &s) // 对象引用作为参数 { if(&s==this) // 不能自己相加,this是当前对象的指针 cout<<"自己不能相加"<<endl; else n+=s.n; } void disp(){ cout<<endl<<" n="<<n<<endl;} }; void main() { Sample s1(10),s2(20),s3(30),s4(40); s1.add(s2); s1.add(s3); s1.add(s4); s1.disp(); cout<<endl; } 本程序的执行结果如下: n=100 --------------------------------------------------- 题8.编写一个程序,设计一个点类Point,求两个点之间的距离。 解: 设计一个普通函数distance(Point &p1,Point &p2),用于计算p1和p2点之间的距离。 本题程序如下: #include<iostream.h> #include<math.h> class Point { int x,y; public: Point(int i,int j){x=i;y=j;} int getx(){ return x;} int gety(){ return y;} void disp() { cout<<"("<<x<<"'"<<y<<")"; } }; float distance(Point &p1,Point &p2) // 对象引用作为参数 { float d; d=sqrt((p1.getx()-p2.getx())*(p1.getx()-p2.getx())+ (p1.gety()-p2.gety())*(p1.gety()-p2.gety())); return d; } void main() { Point p1(2,2),p2(5,5); p1.disp(); cout<<"与"; p2.disp(); cout<<"之间距离="<<distance(p1,p2)<<endl; } 本程序执行结果如下 (2,2) 与 (5,5) 之间距离=4.24264 ----------------------------------------------------- 题9.编写一个程序,设计一个职工类Person,一个系有若干个职工,按职务分为系主任、室主任和职工,给出他们之间的领导关系。 解: 类Person有姓名、职务和指向领导的指针等私有数据,以及两个构造函数和以下成员函数:setleader()(设置当前职工的领导);getname()(获取职工姓名);getleader()(获取领导者对象指针);disp()(输出姓名和职务)。 本题程序如下: #include<iostream.h> #include<stdio.h> #include<string.h> class Person { char name[10]; char prof[10]; Person *leader; public: Person(){strcpy(name,"\0");} Person(char n[],char p[]) { strcpy(name,n); strcpy(prof,p); leader=new Person; } void setleader(Person &p){leader=&p;} // 对象引用作为参数 char *getname(){ return name;} Person *getleader() { return leader; } void disp() { printf("%10s %10s%",name,prof); } }; void main() { Person p[]={Person("王华","室主任"),Person("李明","职工"), Person("陈强","系主任"),Person("章城","职工"), Person("张伟","室主任"),Person("许源","职工")}; p[0].setleader(p[2]); p[1].setleader(p[0]); p[3].setleader(p[4]); p[4].setleader(p[2]); p[5].setleader(p[4]); printf(" 姓名 职务 领导姓名\n"); printf("------------------------------\n"); for(int i=0;i<6;i++) { p.disp(); printf("%10s\n",(p.getleader())->getname()); } } 本程序的执行结果如下: 姓名 职务 领导姓名 ----- ------ ------- 王华 室主任 陈强 李明 职工 王华 陈强 系主任 章城 职工 张伟 张伟 室主任 陈强 许源 职工 张伟 C++的static关键字C++的static关键字
一、面向过程设计中的static 1、静态全局变量 在全局变量前,加上关键字static,该变量就被定义成为一个静态全局变量。我们先举一个静态全局变量的例子,如下: //Example 1
#include <iostream.h>
void fn();
static int n; //定义静态全局变量
void main()
{
n=20;
cout<<n<<endl;
fn();
}
void fn()
{
n++;
cout<<n<<endl;
}
静态全局变量有以下特点:
一般程序的由new产生的动态数据存放在堆区,函数内部的自动变量存放在栈区。自动变量一般会随着函数的退出而释放空间,静态数据(即使是函数内部的静态局部变量)也存放在全局数据区。全局数据区的数据并不会因为函数的退出而释放空间。细心的读者可能会发现,Example 1中的代码中将 static int n; //定义静态全局变量改为 int n; //定义全局变量程序照样正常运行。 的确,定义全局变量就可以实现变量在文件中的共享,但定义静态全局变量还有以下好处:
您可以将上述示例代码改为如下: //Example 2
//File1
#include <iostream.h>
void fn();
static int n; //定义静态全局变量
void main()
{
n=20;
cout<<n<<endl;
fn();
}
//File2
#include <iostream.h>
extern int n;
void fn()
{
n++;
cout<<n<<endl;
}
编译并运行Example 2,您就会发现上述代码可以分别通过编译,但运行时出现错误。试着将 static int n; //定义静态全局变量改为 int n; //定义全局变量再次编译运行程序,细心体会全局变量和静态全局变量的区别。 2、静态局部变量 在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量。 我们先举一个静态局部变量的例子,如下: //Example 3
#include <iostream.h>
void fn();
void main()
{
fn();
fn();
fn();
}
void fn()
{
static n=10;
cout<<n<<endl;
n++;
}
通常,在函数体内定义了一个变量,每当程序运行到该语句时都会给该局部变量分配栈内存。但随着程序退出函数体,系统就会收回栈内存,局部变量也相应失效。但有时候我们需要在两次调用之间对变量的值进行保存。通常的想法是定义一个全局变量来实现。但这样一来,变量已经不再属于函数本身了,不再仅受函数的控制,给程序的维护带来不便。 静态局部变量正好可以解决这个问题。静态局部变量保存在全局数据区,而不是保存在栈中,每次的值保持到下一次调用,直到下次赋新值。 静态局部变量有以下特点:
3、静态函数 在函数的返回类型前加上static关键字,函数即被定义为静态函数。静态函数与普通函数不同,它只能在声明它的文件当中可见,不能被其它文件使用。 静态函数的例子: //Example 4
#include <iostream.h>
static void fn();//声明静态函数
void main()
{
fn();
}
void fn()//定义静态函数
{
int n=10;
cout<<n<<endl;
}
定义静态函数的好处:
二、面向对象的static关键字(类中的static关键字) 1、静态数据成员 在类内数据成员的声明前加上关键字static,该数据成员就是类内的静态数据成员。先举一个静态数据成员的例子。 //Example 5
#include <iostream.h>
class Myclass
{
public:
Myclass(int a,int b,int c);
void GetSum();
private:
int a,b,c;
static int Sum;//声明静态数据成员
};
int Myclass::Sum=0;//定义并初始化静态数据成员
Myclass::Myclass(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
Sum+=a+b+c;
}
void Myclass::GetSum()
{
cout<<"Sum="<<Sum<<endl;
}
void main()
{
Myclass M(1,2,3);
M.GetSum();
Myclass N(4,5,6);
N.GetSum();
M.GetSum();
}
可以看出,静态数据成员有以下特点:
与静态数据成员一样,我们也可以创建一个静态成员函数,它为类的全部服务而不是为某一个类的具体对象服务。静态成员函数与静态数据成员一样,都是类的内部实现,属于类定义的一部分。普通的成员函数一般都隐含了一个this指针,this指针指向类的对象本身,因为普通成员函数总是具体的属于某个类的具体对象的。通常情况下,this是缺省的。如函数fn()实际上是this->fn()。但是与普通函数相比,静态成员函数由于不是与任何的对象相联系,因此它不具有this指针。从这个意义上讲,它无法访问属于类对象的非静态数据成员,也无法访问非静态成员函数,它只能调用其余的静态成员函数。下面举个静态成员函数的例子。 //Example 6
#include <iostream.h>
class Myclass
{
public:
Myclass(int a,int b,int c);
static void GetSum();/声明静态成员函数
private:
int a,b,c;
static int Sum;//声明静态数据成员
};
int Myclass::Sum=0;//定义并初始化静态数据成员
Myclass::Myclass(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
Sum+=a+b+c; //非静态成员函数可以访问静态数据成员
}
void Myclass::GetSum() //静态成员函数的实现
{
// cout<<a<<endl; //错误代码,a是非静态数据成员
cout<<"Sum="<<Sum<<endl;
}
void main()
{
Myclass M(1,2,3);
M.GetSum();
Myclass N(4,5,6);
N.GetSum();
Myclass::GetSum();
}
关于静态成员函数,可以总结为以下几点:
|
||||||||||
|
|