一段简单的代码,运行下自己体会(Cpp与Cs析构函数)
2009-06-06 19:16
375 查看
Cpp代码:
// CppTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
class tree
{
int height;
public:
tree(int initialHeight);
~tree();
void grow(int years);
void printsize();
};
tree::tree(int initialHeight)
{
height=initialHeight;
}
tree::~tree()
{
puts("inside tree destructor");
printsize();
}
void tree::grow(int years)
{
height+=years;
}
void tree::printsize()
{
printf("tree height is %d\n",height);
}
void _tmain(int argc, _TCHAR* argv[])
{
puts("before opening brace");
{
tree t(12);
puts("after tree creation");
t.printsize();
t.grow(4);
puts("before closing brace");
}
puts("after closing brace");
}
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSTest
{
public class Tree
{
int height;
public Tree(int initialHeight)
{
height = initialHeight;
}
~Tree()
{
Console.WriteLine("inside tree destructor");
printsize();
}
public void grow(int years)
{
height += years;
}
public void printsize()
{
Console.WriteLine("tree height is {0}",height);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("before openint brace");//原来C#这样写代码段也可以
{
Tree t=new Tree(12);
Console.WriteLine("after tree creation");
t.printsize();
t.grow(4);
Console.WriteLine("before closing brace");
}
Console.WriteLine("after closing brace");
}
}
}
不要忘记把想法写出来哦,哈哈。
// CppTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
class tree
{
int height;
public:
tree(int initialHeight);
~tree();
void grow(int years);
void printsize();
};
tree::tree(int initialHeight)
{
height=initialHeight;
}
tree::~tree()
{
puts("inside tree destructor");
printsize();
}
void tree::grow(int years)
{
height+=years;
}
void tree::printsize()
{
printf("tree height is %d\n",height);
}
void _tmain(int argc, _TCHAR* argv[])
{
puts("before opening brace");
{
tree t(12);
puts("after tree creation");
t.printsize();
t.grow(4);
puts("before closing brace");
}
puts("after closing brace");
}
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSTest
{
public class Tree
{
int height;
public Tree(int initialHeight)
{
height = initialHeight;
}
~Tree()
{
Console.WriteLine("inside tree destructor");
printsize();
}
public void grow(int years)
{
height += years;
}
public void printsize()
{
Console.WriteLine("tree height is {0}",height);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("before openint brace");//原来C#这样写代码段也可以
{
Tree t=new Tree(12);
Console.WriteLine("after tree creation");
t.printsize();
t.grow(4);
Console.WriteLine("before closing brace");
}
Console.WriteLine("after closing brace");
}
}
}
不要忘记把想法写出来哦,哈哈。
相关文章推荐
- 你应当如何学习C++
- c语言版汇款系统
- 学C++的50个忠告
- C语言编译全过程
- C++类对象的复制-拷贝构造函数
- 孙鑫VC++笔记_第一课_SDK编程
- 类C语言的 BNF(backus-Naur Form)文法
- C++重载运算符
- c++ setlocale函数
- assert() c++ 使用方法
- c++ 程序 统计一个字符串内的正整数和负整数个数
- C++ 发声函数如何成功使用
- 快乐学C++:(5) 对象的初始化
- 如何理解c和c++的复杂类型声明
- vc++实现控制USB设备启用与否
- 百度c语言贴吧 经典C源程序100例-18
- 百度c语言贴吧 经典C源程序100例-16
- C++入门解惑——为什么学习C++
- 百度c语言贴吧 经典C源程序100例-14
- C语言之#define用法