C++类的封装_工程
一个C++工程
main.cpp
#include<stdio.h>
#include"Array.h"
int main()
{
Array a1(10);
for(int i=0;i<a1.length();i++)
{
a1.setdata(i,i);
}
for (int j=0; j<a1.length(); j++)
{
printf("Element %d : %d \n ",j,a1.getdata(j));
}
a1.destroy();
printf("press any key to continue...");
getchar();
return 0;
}
Array.h
#ifndef _Array_H_
#define _Array_H_
class Array
{
private:
int mlength;
int* mspace;
public:
Array(int length); //构造函数
Array(const Array& obj); //构造函数(实现复制功能)
int length();
void setdata(int index, int value);
int getdata(int index);
~Array();
};
#endif
Array.cpp
#include "Array.h"
Array::Array(int length)
{
if(length<0)
{
length=0;
}
mlength=length;
mspace=new int[mlength];
}
Array::Array(const Array& obj)
{
mlength=obj.mlength;
mspace=new int(mlength);
for(int i=0; i<mlength;i++)
{
mspace[i]=obj.mspace[i];
}
}
int Array::length()
{
return mlength;
}
void Array::setdata(int index, int value)
{
mspace[index]=value;
}
int Array::getdata(int index)
{
return mspace[index];
}
Array::~Array() ///析构函数 销毁对象前 释放内存 自动调用
{
mlength=-1;
delete[] mspace;
}
C++类的封装_工程的更多相关文章
- jsp_类的封装_集合的应用
一.需求分析 做一个jsp页面,动态显示信息表的内容. 1.做一个实体类:StudentInfo(包含4个字段) 2.如图模拟生成3条数据,本质上就是new StudentInfo 3个实例, 每一个 ...
- 022医疗项目-模块二:药品目录的导入导出-对XSSF导出excel类进行封装
资源全部来源于传智播客. 好的架构师写的程序,就算给刚入门的新手看,新手一看就知道怎么去用.所以我们要对XSSF导出excel类进行封装.这是架构师的工作,但我们也要知道. 我们写一个封装类: 这个类 ...
- 抽象类,接口类,封装,property,classmetod,statimethod
抽象类,接口类,封装,property,classmetod,statimethod(类方法,静态方法) 一丶抽象类和接口类 接口类(不崇尚用) 接口类:是规范子类的一个模板,只要接口类中定义的,就应 ...
- 孤荷凌寒自学python第二十三天python类的封装
孤荷凌寒自学python第二十三天python类的封装 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.从怎么样访问类的内部代码块中定义的私有属性说起 类中定义的私有属性和私有方法是无法被 ...
- python 类的封装/property类型/和对象的绑定与非绑定方法
目录 类的封装 类的property特性 类与对象的绑定方法与非绑定方法 类的封装 封装: 就是打包,封起来,装起来,把你丢进袋子里,然后用绳子把袋子绑紧,你还能拿到袋子里的那个人吗? 1.隐藏属性和 ...
- 类的封装,property特性,类与对象的绑定方法和非绑定方法,
类的封装 就是把数据或者方法封装起来 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度(快门就是傻瓜相机为傻瓜们提供的方法,该方法将内部复杂的照相功能都隐藏起来了,比如你 ...
- swift项目第十天:网络请求工具类的封装
import UIKit /* 必须先导入头文件:import AFNetworking */ import AFNetworking //MARK:-0:定义枚举:以枚举定义请求网络的get和pos ...
- GD库的基本信息,图像的旋转、水印、缩略图、验证码,以及图像类的封装
GD库检测 <?php phpinfo(); ?> GD库安装• Windows 使用phpstudy • Linux 编译安装 –with-gd• Linux 编译安装扩展 GD库支持的 ...
- Python 基础之面向对象初识与类的封装
一.面向对象类的初识 1.类的定义 #三种方式:#1.class MyClass: pass #2.推荐class MyClass(): pass #3.class MyClass(obj ...
随机推荐
- 监控Informix-Url
jdbc:informix-sqli://[{ip-address|host-name}:{port-number|service-name}][/dbname]: INFORMIXSERVER=se ...
- Dialog with HTML skin using CDHtmlDialog and SetWindowRgn
Introduction This program demonstrates how to use CDHtmlDialog and SetWindowRgn functions to give a ...
- highlight a DOM element on mouse over, like inspect does
highlight a DOM element on mouse over, like inspect does highlight a DOM element on mouse over, like ...
- poj 2406 Power Strings(KMP变形)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28102 Accepted: 11755 D ...
- android 隐藏API 在源码下编译报错cannot find symbol symbol
应该是我对android 不熟悉的缘故,今天使用源码编译了一个调用了隐藏api的应用程序始终报错: cannot find symbol symbol : class IPackageInstall ...
- CreateFileMapping使用方法
CreateFileMapping的MSDN翻译和使用心得 測试创建和打开文件映射的时候老是得到"句柄无效"的错误, 细致看了MSDN以后才发觉是函数认识不透, 这里把相关的解 ...
- Contributing to Open Source on GitHub(转)
A great way to get involved in open source is to contribute to the existing projects you’re using. G ...
- Android 开发笔记“Eclipse 调试和快捷键”
原文地址:http://blog.sina.com.cn/s/blog_5cf876340100aswr.html Eclipse 调试器和 Debug 视图 Eclipse SDK 是针对 Java ...
- Oracle分区知识
查询分区名称.表空间的SQL USER_SEGMENTS SELECT SEGMENT_NAME,PARTITION_NAME,TABLESPACE_NAME FROM USER_SEGMENTS; ...
- 关于Class.forName(“com.mysql.jdbc.Driver”)--转
传统的使用jdbc来访问数据库的流程为:Class.forName(“com.mysql.jdbc.Driver”);String url = “jdbc:mysql://localhost:3306 ...