云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 2.1 类和对象(第二章 C++面向对象程序设计)

2.1 类和对象(第二章 C++面向对象程序设计)

  • 62 次阅读
  • 3 次下载
  • 2025/5/1 11:38:47

构造完整的程序,包括类成员函数的定义和测试程序的设计,构造函数中的nIntSize和nFloatSize分别表示整型数组和浮点数组的大小。

#include using namespace std;

class CArray {

int nSize0fInt; int nNum0fInt; int nSize0fFloat; int nNum0fFloat; int *pInt; float *pFloat; public:

CArray(int nIntSize=100,int nFloatSize=200); void put(int n); void put(float x); int getInt(int index); float getFloat(int index); ~CArray(); void print(); };

CArray::CArray(int nIntSize,int nFloatSize) {

pInt=new int[nIntSize]; pFloat=new float[nFloatSize]; nSize0fInt=nIntSize; nSize0fFloat=nFloatSize; nNum0fInt=0; nNum0fFloat=0; for(int i=0;i<9;i++) {

pInt[i]=0; nNum0fInt++; }

for( i=0;i<9;i++)

{

pFloat[i]=0.0; nNum0fFloat++; } }

void CArray::put(int n) {

pInt[nNum0fInt++]=n; }

void CArray::put(float x) {

pFloat[nNum0fFloat++]=x; }

int CArray::getInt(int index) {

return pInt[index]; }

float CArray::getFloat(int index) {

return pFloat[index]; }

CArray::~CArray() {

delete [] pInt; delete [] pFloat; }

void CArray::print() {

for(int i=0;i

cout<

for( i=0;i

cout<

}

void main() {

CArray one; one.put(10); one.put(12.3f); one.getInt(2); one.getFloat(6); one.print(); }

19、在一个程序中,实现如下要求: (1)构造函数重载;

(2)成员函数设置默认参数; (3)有一个友元函数; (4)有一个静态函数;

(5)使用不同的构造函数创建的对象。 #include using namespace std;

class Sample {

friend void frd_function(); public:

Sample():x(0) { } Sample(int i):x(i) { } int set(int a=2) {

x=a; return x; }

static void _static() {

cout << \ } private:

int x; };

void frd_function() {

Sample a;

cout << a.x << endl; }

void main() {

Sample a; Sample b(3); a._static();

cout << a.set() << endl; frd_function(); }

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

构造完整的程序,包括类成员函数的定义和测试程序的设计,构造函数中的nIntSize和nFloatSize分别表示整型数组和浮点数组的大小。 #include using namespace std; class CArray { int nSize0fInt; int nNum0fInt; int nSize0fFloat; int nNum0fFloat; int *pInt; float *pFloat; public: CArray(int nIntSize=100,int nFloatSize=200); void put(int n); void put(float x)

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com