![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++程序
文章平均质量分 84
协议森林
这个作者很懒,什么都没留下…
展开
-
【c++程序】成员函数的重载(示例)
#includeusing namespace std;class cube{public: void sum(); void sum(int x,int y );private: int i; int j;};int main(){ cube a; a.sum(2,3); a.sum(); return 0;}void cube::sum(){原创 2014-11-14 21:00:38 · 591 阅读 · 0 评论 -
【c++程序】重载构造函数(示例)
#includeusing namespace std;class rectangle {public: rectangle() { cout<<"构造一个长方形a!"<<endl; } rectangle(int l,int w) { length=l;width=w; cout<<"长方形的面积为"<<length*width<<endl; } rect原创 2014-11-16 16:14:19 · 606 阅读 · 0 评论 -
【c++程序】成员变量的初始化与构造函数(示例)
#includeusing namespace std;class demo {public: demo(){x=0;cout<<"demo的默认构造"<<x<<endl;} demo(int i){x=i;cout<<"demo的带一个参量的构造函数"<<x<<endl;} ~demo(){cout<<"demo的默认析构函数"<<x<<endl;} int get(){retu原创 2014-11-17 15:40:40 · 547 阅读 · 0 评论 -
【c++程序】用起泡法对十个数排序(由小到大)
#includeusing namespace std;int main(){ int a[10]; int i,j,k; cout<<"请输入十个数字:"<<endl; for(i=0;i<10;i++) cin>>a[i]; cout<<endl; for(j=0;j<9;j++) for(i=0;i<9-j;i++) if原创 2014-11-18 16:57:17 · 5680 阅读 · 1 评论 -
【c++程序】通过别名来交换两个数的值
/* *文件名称:pointer.cpp *作 者:Zhao Tilu *完成日期:2014年10月15日 *问题描述:通过指针来交换两个数的值 */原创 2014-10-15 15:51:35 · 657 阅读 · 0 评论 -
【c++程序】求解一元二次方程
#include<iostream>#include <math.h>using namespace std;int main(){ double a,b,c,d,x,y; cout<<"欢迎使用锤子Ethan解方程,一般式为ax^2+bx+c=0"<<endl; cout<<"请输入系数a,b,c。"<<en...原创 2014-11-19 20:44:49 · 13085 阅读 · 0 评论 -
【c++程序】调用构造函数进行类型转换
#includeusing namespace std;class A{public: explicit A(int x){i=x;cout<<"执行构造函数"<<i<<endl;} ~A(){cout<<"执行析构函数"<<i<<endl;} void get(){cout<<i<<endl;}private: int i;};int main(){ A a(9原创 2014-11-20 16:23:42 · 483 阅读 · 0 评论 -
【c++程序】引用容易犯得错误
#includeusing namespace std;class A{public: A(int i){cout<<"执行构造函数创建一个对象\n";x=i;} A(A&a){x=a.x;cout<<"执行复制构造函数创建一个对象\n";} ~A(){cout<<"执行析构函数!\n";} int get(){return x;}private: int原创 2014-10-18 20:34:46 · 681 阅读 · 0 评论 -
【c++程序】运算符的重载
#includeusing namespace std;class num{public: num(){n=1;}//构造函数 ~num(){} //析构函数 int get() const{return n;} void set(int x){n=x;} void add(){++n;} void operator++(){++n;}//没有返回值,调用重载运算符oper原创 2014-11-26 20:05:15 · 393 阅读 · 0 评论 -
【c++程序】浅层复制构造函数
/* 编译器提供默认构造函数和析构函数,同样也提供复制构造函数, 编译器提供的默认复制构造函数只是把传递进来的每一个成员变量 复制到新对象的成员变量中,然后两个对象中的变量均指向存放类 成员变量的那块内存区域。 */#includeusing namespace std;class A{public: A(){x=new int;*x=5;} ~A(){del原创 2014-11-27 14:56:44 · 439 阅读 · 0 评论 -
【c++程序】关于指针易混淆的几个概念
#includeusing namespace std;int main(){ int i; int *p=0; cout<<"i的地址为"<<&i<<endl; cout<<"p的值为"<<p<<endl; cout<<"p的内存地址"<<&p<<endl; i=3; cout<<"i的地址为:"<<&i<<endl; p=&i; co原创 2014-11-30 15:30:46 · 423 阅读 · 0 评论 -
【c++程序】深层复制构造函数
/*为了解决浅层复制导致的迷途指针问题,必须创建自己的复制构造函数, 并且在函数里为我们的成员变量分配内存。这样,在分配完内存后,旧对 象的成员变量就可以复制到新的内存区域中,两个对象的成员变量都各自 拥有自己的内存的区域,一个对象析构后不影响到另一个,我们把这种复 制方式叫做深层复制*/#includeusing namespace std;class A{public原创 2014-11-29 15:30:18 · 588 阅读 · 0 评论 -
【c++程序】模拟抽象类
#includeusing namespace std;class Human{public: Human(){cout<<"构造Human"<<endl;} virtual void smart(){} virtual void beautiful(){} virtual ~Human(){cout<<"析构Human"<<endl;}};class father:virtu原创 2015-01-06 20:51:44 · 466 阅读 · 0 评论 -
【c++程序】取消创建临时对象
#includeusing namespace std;class num{public: num(){n=1;cout<<"构造函数执行"<<endl;}//构造函数 num(int i){n=i;cout<<"带参数的构造函数执行"<<endl;} num(const num&s){this->n=s.n;cout<<"复制构造函数执行"<<endl;} ~num(){cout原创 2014-12-01 16:43:36 · 494 阅读 · 0 评论 -
【c++程序】图书馆购书系统(尚存问题)
//无法正常运行#include#includeusing namespace std;class book{public: int num; float price; book *next;};book *head=NULL;bool check(string str)//检查是否是数字{ for(int i=0;i<str.length();i++) { if原创 2015-01-26 17:51:09 · 1124 阅读 · 0 评论 -
【c++程序】继承的构造与析构
#includeusing namespace std;class father{private: int a;public: father(int i){a=i;cout<<"构造基类a的值:"<<a<<endl;} ~father(){cout<<"析构基类a的值:"<<a<<endl;}};class son:public father{private: int b原创 2014-12-16 19:23:46 · 446 阅读 · 0 评论 -
【c++程序】重载加法运算符operator+
/*既然使用关键字operator配合运算符++可以实现对象的自加, 那么关键字operator配合+就可实现对象相加*/#includeusing namespace std;class num{public: num(){n=1;cout<<"构造函数执行"<<endl;}//构造函数 num(int i){n=i;cout<<"带参数的构造函数执行"<<endl;} ~n原创 2014-12-02 16:34:50 · 8398 阅读 · 0 评论 -
【c++程序】动态链表的建立
#includeusing namespace std;class book{public: int num; float price; book *next;};book *head=NULL;book *creat(){ book *p1,*p2; p1=new book; head=p1; p2=p1; cout<<"请输入图书的编号,以0结束"<<endl;原创 2015-01-26 16:04:51 · 6287 阅读 · 0 评论 -
【c++程序】求一个数的阶乘n!
#includeusing namespace std;long fact(int n);int main(){ int n; long y; cin>>n; y=fact(n); cout<<n<<"!="<<y<<endl; return 0;}long fact(int n){ long f; if(n==1) f=1; e原创 2014-12-03 21:09:17 · 4282 阅读 · 0 评论 -
【c++程序】函数指针
#include#includeusing namespace std;float (*fp)(float&,float&);void (*p)(float &,float &);float triangle(float &x,float &y){ return x*y*0.5;}float rectangle(float &x,float &y){ return x*y;原创 2015-01-29 16:25:32 · 616 阅读 · 0 评论 -
【c++程序】静态成员的使用
#includeusing namespace std;class aspl{public: aspl(float p){price=p;TotalPrice=p+TotalPrice;} ~aspl(){TotalPrice=TotalPrice-price;} static float get(){return TotalPrice;}private: float price原创 2015-01-28 16:49:29 · 583 阅读 · 0 评论 -
【c++程序】汉诺塔
#includeusing namespace std;void hano(int n,char form,char temp,char to);int main(){ hano(3,'A','C','B'); return 0;}void hano(int n,char form,char temp,char to)//若有默认值需要注释起来{ if(n==1) cout原创 2015-01-11 20:54:30 · 717 阅读 · 0 评论 -
【c++程序】抽象类的应用
#includeusing namespace std;class Shape{public: virtual double area()=0;//纯虚函数};class Trigon:public Shape {public: Trigon(double H,double W) { h=H; w=W; } double area() { return h*原创 2015-01-14 11:02:18 · 911 阅读 · 0 评论 -
【c++程序】复杂抽象结构
#includeusing namespace std;class Animal{public: Animal(int); virtual ~Animal(){cout<<"析构动物"<<endl;} virtual int GetAge(){return itsage;} virtual void Sleep()=0; virtual void Propagate()=0;原创 2015-01-14 20:48:53 · 733 阅读 · 0 评论 -
【c++程序】拳击游戏
#includeusing namespace std;class poser{public: virtual void beat()const {cout<<"一般选手的力量260磅"<<endl;}protected: int age;};class Ali:public poser{public: void beat()const{cout<<"阿里一拳的力量为420原创 2015-01-02 21:16:39 · 1078 阅读 · 0 评论 -
【c++程序】静态局部变量
#includeusing namespace std;int counter(){ static int a=0; ++a; return a;}int main(){ cout<<counter()<<endl; cout<<counter()<<endl; cout<<counter()<<endl; return 0;}原创 2015-01-13 21:38:29 · 516 阅读 · 0 评论 -
【c++程序】数组排列大小方法
#includeusing namespace std;int main(){ int const N=5; int a[5]={12,14,11,3,5}; for(int i=0;i<N;i++) for(int j=i+1;j<N;j++) { if(a[i]>a[j]) swap(a[i],a[j]);//调用了标准库algorithm里的函数 }原创 2015-02-03 20:36:55 · 9861 阅读 · 0 评论 -
【c++程序】求平均成绩
#define num 10 //预定义,好修改女生的数量#includeusing namespace std;int main(){ int girl[num]; int total=0,i; float average; cout<<"请输入"<<num<<"名女生的分数"<<endl; for(i=0;i<num;i++) { cout<<"编号为"<<i+1原创 2015-01-15 11:22:44 · 6235 阅读 · 0 评论 -
【c++程序】继承公有型,保护型和私有型
#includeusing namespace std;class father{protected: int fatherheight,fatherweight;//父类只想要子类的成员访问,用关键字protectedpublic: void setfatherheight(int fathersheight){fatherheight=fathersheight;} void原创 2014-12-11 19:06:26 · 560 阅读 · 0 评论 -
【c++程序】多重继承
#includeusing namespace std;class father{public: void setA(int a){tall=a;} void printA(){cout<<"身高="<<tall<<endl;}private: int tall;};class mother{public: void setB(int a){weight=a;} voi原创 2014-12-11 19:55:06 · 445 阅读 · 0 评论 -
【c++程序】向基类构造函数传递参数
#include#includeusing namespace std;class father{protected: string name; int tall;public: father(string a,int i); father(){cout<<"构造基类"<<endl;} void print(){cout<<name<<"身高为:"<<tall<<endl;}原创 2014-12-29 15:37:34 · 1396 阅读 · 0 评论 -
【c++程序】函数接收数组三种方式
#includeusing namespace std;int func1(int x[]){ x[0]=100; return x[0];}int func2(int x[30]){ x[0]=100; return x[0];}int func3(int *x){ x[0]=100; return x[0];}int main(){ int x[30];原创 2015-01-18 16:45:44 · 2932 阅读 · 0 评论 -
【c++程序】数组100到199之间的平均数
#includeusing namespace std;float average(int *,int);int main(){ int a[100]; float Average; int num=100; for(int i=0;i<100;i++) { a[i]=num; num++; cout<<a[i]<<endl; } Average=average(原创 2015-01-18 20:04:30 · 682 阅读 · 0 评论 -
【c++程序】多重继承容易产生两义性
#includeusing namespace std;class a{public: void hello(){cout<<"基类a的hello函数"<<endl;}};class b{public: void hello(){cout<<"基类b的hello函数"<<endl;} void print(){cout<<"基类b的print函数"<<endl;}};cl原创 2014-12-31 14:35:32 · 361 阅读 · 0 评论 -
【c++程序】用二分法查找数据
#includeusing namespace std;int find(int,int[],int);int main(){ while(1) { int a[]={1,32,55,67,68,70,70,71,73,82,101,198,200,201};//要是有重数,则不知返回哪个 int b; cout<<"请输入要查找的数据:"; cin>>b; int原创 2015-01-20 14:59:37 · 1722 阅读 · 0 评论 -
【c++程序】用递增法在数组中查找数据
#includeusing namespace std;int find(int,int[],int);int main(){ while(1) { int a[]={44,32,55,64,34,43,22,98}; cout<<"请输入要查找的数据"; int data; cin>>data; int check=find(data,a,8); if(fin原创 2015-01-19 20:51:33 · 851 阅读 · 0 评论 -
【c++程序】指向对象的指针
#includeusing namespace std;class father{public: father():age(54){cout<<"调用父类的构造函数,并初始化的值为"<<age<<endl;} ~father(){cout<<"调用父类的析构函数"<<endl;} void jump()const {cout<<"父亲可以跳五个台阶"<<endl;} void ru原创 2015-01-02 19:49:51 · 408 阅读 · 0 评论 -
【c++程序】判断数组的排列方式
#includeusing namespace std;int find(int,int [],int);int find1(int,int [],int);bool add(int a[],int n);int main(){ int a[]={22,44,66,88,1,32,64}; if(add(a,7)==1) { cout<<"采用二分算法得到的位置为"<<fi原创 2015-01-21 15:20:01 · 1090 阅读 · 0 评论 -
【c++程序】静态联编
#includeusing namespace std;class A{public: int get(){return 1;}};class B:public A{public: int get(){return 2;}};int main(){ A a; int one; one=a.get(); cout<<"a的值是"<<one<<endl; B b;原创 2015-01-03 14:58:23 · 429 阅读 · 0 评论 -
【c++程序】在数组对象中初始化成员变量
#includeusing namespace std;class area{public: area(int l,int w){length=l;width=w;cout<<"调用构造函数,设置长和宽:"<<length<<"\t"<<width<<endl;} ~area(){cout<<"调用析构函数释放内存,此时长和宽:"<<length<<"\t"<<width<<endl;原创 2015-01-21 16:13:28 · 1177 阅读 · 0 评论