05 2016 档案
摘要:#include "stdafx.h" #include #include #include using namespace std; class CSelsctLesson { public: CSelsctLesson(); CSelsctLesson( string lesson ) { LessonName = lesson;...
阅读全文
摘要:#include "stdafx.h" #include using namespace std; class Term { public: const int x; Term( int c); }; /* Term::Term(int c) { x = c; //错误,不能采用在构造函数中对常数据成员赋初值的方法进行初始化 } */ Term::Term(in...
阅读全文
摘要:// this.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include using namespace std; class A { public: A() { i = 0; } void add() { i++;} A*sp() { return this; } int Is(A*s) { return s->i; } private: i...
阅读全文
摘要:有一个点类point,具有两个实数坐标,希望主程序使用这个类完成一下功能: 1.主程序为类point申请10个连续的存储空间 2.调用一个函数Set()从键盘输入10个对象的属性,并顺序存入申请的内存中 3.调用一个函数Display()显示10个对象的值 4.调用一个函数Length(),计算将这些点连成一条折线时,这条折线的长度 5.程序结束时,删除申请的内存 6.演示析构函数(动态对象或堆对...
阅读全文
摘要:#include "stdafx.h"#includeusing namespace std;class Box{ int height; int width; int length;public: Box(); Box(int h, int w, int l) :height(h), width(w), length(l) {} int volume(); ...
阅读全文
摘要:// 对象指针.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#includeusing namespace std;class Student{ int id; char name[10]; float score;public: Student(int pid, char *pname, float s); void disp...
阅读全文
摘要:// 队列操作类.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include using namespace std; struct list { int data; list *next; }; class Queue { public: Queue() { ...
阅读全文
摘要://如果不显示定义复制构造函数,编译会出错,原因是:在创建对象s2时,调用默认复制构造函数并用对象s1对其进行初始化,致使s2中指针 //与s1中指针指向同一储存空间,当一个对象生命周期结束后调用析构函数释放内存空间后,另一个变量的指针悬空,无法正常使用。 //浅复制 //再用一个对象初始化另一个对象时,只复制了成员,没有复制资源(指堆内存 ,数据成员没有具体值),使两个对象同时指向同一资源, /...
阅读全文
摘要:// 对象做函数参数和返回值.cpp : 定义控制台应用程序的入口点。//exit(0)表示正常退出程序,exit(0)表示异常退出 //在调用input时,编译器用对象A去创建了形参对象temp,调用了复制构造函数,对象A中的数据复制给了对象temp// 在input函数中,执行temp.set(s),为对象temp中数据成员str申请了动态储存空间,并设置了输入的字符串//并没有改变实参A中...
阅读全文
摘要:// 复制构造函数.cpp : 定义控制台应用程序的入口点。//复制构造函数:类(const 类&对象(随便起))/*Box(const Box&box){length = box.length;width = box.width;height = box.height;}*/ #include "stdafx.h"#includeusing namespace std;class Box{pri...
阅读全文
摘要:// 构造函数与析构函数2.cpp : 定义控制台应用程序的入口点。//学习动态内存单元的申请 #include "stdafx.h"#includeusing namespace std;class Student{public: Student(); Student(int pid, char*pname, float s); void modify(float s); ...
阅读全文
摘要:// 构造函数与析构函数.cpp : 定义控制台应用程序的入口点。//要学习的两点://要想表现出析构函数,必须在对象外面加大括号!!// while (cin.get() != '\n') num++;#include "stdafx.h"#includeusing namespace std;class Count{public: Count(); ~Count(); ...
阅读全文
摘要://构造函数含有含默认值的参数 #include "stdafx.h"#includeusing namespace std;class Box{public: Box(int w = 10, int h = 10, int len = 10); int volume();private: int height; int width; int length;}; Bo...
阅读全文
摘要:// simple stack.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#includeusing namespace std;const int SIZE = 100;class Stack{ public: void init() { position = 0; } ...
阅读全文
摘要:// test.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#includeusing namespace std;const int i = 100;const int j = i * 3; int main(){ const int *x; x = &i;//x是一个指针,它指向一个const int,即x可以指向任何标识符(即它不是...
阅读全文
摘要:先来一张Google与nexus,加油啦! 用构造函数确保初始化 下面是一个带构造函数的类的简单例子 class X { int i; public: X(); //constructor } 当一个对象被定义时: void f() { X a; } 此时a就好像是一个int:为这个对象分配内存,但是当程序执行到a的序列点时,构造函数自动调用,传递到构造函数的第一个参数(秘密)是this指针...
阅读全文
摘要:// 构造函数2.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#includeusing namespace std;class Box{public: Box(); Box(int, int, int); //声明带参数的构造函数 int volume(); //声明计算体积的函数pri...
阅读全文
摘要:类是一种复杂的数据类型,它是将不同类型的数据和与这些数据相关的操作封装在一起的集合体。这有点像C语言中的结构,唯一不同的就是结构没有定义所说的“数据相关的操作”,“数据相关的操作”就是我们平常经常看到的“方法”,因此,类具有更高的抽象性,类中的数据具有隐藏性,类还具有封装性。 类的结构(也即类的组成)是用来确定一类对象的行为的,而这些行为是通过类的内部数据结构和相关的操作来确定的。这些行为是通...
阅读全文
摘要:一、预备知识―程序的内存分配 一个由C/C++编译的程序(更严密的说应该是进程)占用的内存分为以下几个部分 1、栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 2、堆区(heap) ― 一般由程序员分配释放, 若程序员不释放,程序结束
阅读全文

浙公网安备 33010602011771号