没有合适的资源?快使用搜索试试~ 我知道了~
C++编程题目专项训练
3星 · 超过75%的资源 需积分: 3 15 下载量 160 浏览量
2010-11-02
09:51:07
上传
评论 1
收藏 144KB DOC 举报
温馨提示
试读
25页
C++编程题目专项训练
资源推荐
资源详情
资源评论
C++备课编程题目专项训练
第一章_总论
1.找错
class base{
int *p;
public:
base (int a){p=&a};
int Getx(){return m;}
~base(){delete p}
};
答案:
base (int a){p=&a};应该改为 base (int a){p=&a;}
return m;---错误,没又定义变量 m;
~base(){delete p}应该加;
/*
2008 年 10 月份 C++ 编程题目汇总
涉及的知识点:
1. 形参与实参对应关系
*/
#include <iostream>
using namespace std;
class f
{
private:
int x; //类的成员属性(数据成员)不能直接赋值,需要使用构造函数来赋值。
int y;
public :
void f1()
{
x=0;
y=0;
}
void print()
{
cout<<x<<" "<<y<<endl;
}
};
void main2()
{
f a;
a.f1(); //写成 a.f1(1,1) 就会报错,形参与实参不一致。
a.print();
}
/*
2008 年 10 月份 C++ 编程题目汇总
涉及的知识点:
1. 类的引用
2. 封装性 private,public
*/
#include <iostream>
using namespace std;
class point
{
public: //如果是 private 则本例题不能编译执行。
float x;
public:
void f()
{
x=0;
}
void f(float a)
{
x=a;
}
};
float max(point& a,point& b) //类的引用,不能访问类的私有成员数据(成员属性)
{
return (a.x>b.x)?a.x:b.x;
}
void main3()
{
point a,b; //实例化
a.f(2.2);
b.f(6.8);
cout<<max(a,b)<<endl;
}
第二章_从结构到类.
1. 使用多种方法编写将两个字符串连接在一起的程序。
#include <iostream>
#include <string>
using namespace std;
void main()
{
//使用 string 类定义字符串,完成字符串连接
string str1("C++"),str2("程序设计");
string str3;
str3 = str1+str2;//连接方式 1
cout<< str3<< endl;
//使用 char 数组定义字符串,完成连接
char c1[] = {"c++"},c2[] = {"program"};
char c3[20];
int i=0,k=0;
for(i=0;i<20;i++)//初始化 c3
c3[i]='\0';
i=0;
while(c1[i]!='\0')
{
c3[k]=c1[i];
i++;
k++;
}
i=0;
while(c2[i]!='\0')
{
c3[k]=c2[i];
i++;
k++;
}
cout<< c3<< endl;
}
2. 程序运行,
#include<iostream>
using namespace std;
class Point{
private:
int x,y;
public:
Point(int a=0,int b=0){x=a;y=b;cout<<"Initializing"<<endl;}
Point(const Point &p);
int Gegx(){return x;}
int Getx(){return y;}
void Show(){cout<<"x="<<x<<",y="<<y<<endl;}
~Point(){cout<<"delete..."<<x<<","<<y<<endl;}
};
Point::Point(const Point &p)
{
x=p.x;
y=p.y;
cout<<"copy initialzing"<<endl;
}
void display(Point p)
{p.Show();}
void disp(Point &p)
{p.Show();}
Point fun()
{
Point A(101,202);
return A;
}
void main()
{
Point A(42,35);
Point B(A);
Point C(58,94);
cout<<"called dislay(B)"<<endl;
display(B);
剩余24页未读,继续阅读
资源评论
- 干干的水2014-06-30题目还不错
ywsxen
- 粉丝: 13
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功