C++语言基础 例程 对象的动态建立和释放

贺老师的教学链接  本课讲解

对象的动态建立和释放

#include<iostream>
using namespace std;
class Box
{
public:
    Box(int w,int l,int h);
    ~Box();
    int width;
    int length;
    int height;
};
Box::Box(int w,int l,int h)
{
    width=w;
    length=l;
    height=h;
    cout<<"========调用构造函数=======\n";
}
Box::~Box()
{
    cout<<"========调用析构函数=======\n";
}
int main()
{
    Box * p=new Box(12,13,15);
    cout<<p->width<<"\t";
    cout<<p->length<<"\t";
    cout<<p->height<<endl;
    delete p;
    return 0;
}

建立指针数组

#include <iostream>
using namespace std;
class Time
{
public:
    Time(int,int,int);
    void get_time( );
private:
    int hour,minute,sec;
};
Time::Time(int h,int m,int s):hour(h),minute(m),sec(s) {}
int main()
{
    const int N = 100;
    Time *t[N]= {NULL};
    int i;
    i=3;
    if(t[i]==NULL)
        t[i] = new Time(10, 10,10);
    if(t[i]!=NULL)
        delete t[i];
    return 0;
}
时间: 2024-07-31 08:07:49

C++语言基础 例程 对象的动态建立和释放的相关文章

C++语言基础 例程 对象成员的引用

贺老师的教学链接  本课讲解 通过对象名和成员运算符访问对象中的成员 #include <iostream> using namespace std; class Time { public: void set_time( ); void show_time( ); private: int hour; int minute; int sec; }; int main( ) { Time t1; t1.set_time( ); t1.show_time( ); Time t2; t2.set_

C++语言基础 例程 对象数组

贺老师的教学链接  本课讲解 对象数组应用实例 #include <iostream> using namespace std; class Box { public: Box(int h=10,int w=12,int len=15): height(h),width(w),length(len) { } int volume( ); private: int height; int width; int length; }; int Box::volume( ) { return(heig

C++语言基础 例程 对象指针

贺老师的教学链接  本课讲解 示例:使用指向对象数据成员的指针 #include <iostream> using namespace std; class Time { public: Time(int,int,int); void get_time( ); private: int hour,minute,sec; }; Time::Time(int h,int m,int s):hour(h),minute(m),sec(s) {} void Time::get_time( ) { co

C++中对象的常引用、动态建立和释放相关知识讲解_C 语言

C++对象的常引用 我们知道,一个变量的引用就是变量的别名.实质上,变量名和引用名都指向同一段内存单元. 如果形参为变量的引用名,实参为变量名,则在调用函数进行虚实结合时,并不是为形参另外开辟一个存储空间(常称为建立实参的一个拷贝), 而是把实参变量的地址传给形参(引用名),这样引用名也指向实参变量. [例] 对象的常引用. #include <iostream> using namespace std; class Time { public: Time(int,int,int); int

C++语言基础 例程 抽象类

贺老师的教学链接  本课讲解 实例:顶层的Shape作为抽象类 #include <iostream> using namespace std; //声明抽象基类Shape class Shape { public: virtual float area( ) const { return 0.0; //虚函数 } virtual float volume() const { return 0.0; //虚函数 } virtual void shapeName() const =0; //纯虚

C++语言基础 例程 类和对象的简单应用举例

贺老师的教学链接  本课讲解 实例1:求出三角形的周长和面积 #include<iostream> #include<Cmath> #include<cstdlib> using namespace std; class Triangle { public: void setABC(double x, double y, double z);//置三边的值,注意要能成三角形 double perimeter();//计算三角形的周长 double area();//计算

C++语言基础 例程 初见对象

贺老师的教学链接  本课讲解 问题:求圆柱体积 //面向过程 #include <iostream> using namespace std; int main() { double r, h, v; cin>>r>>h; v = 3.14 * r * r * h; cout<<v<<endl; return 0; } //基于对象 #include <iostream> using namespace std; class Cup{

C++语言基础 例程 类的声明和对象的定义

贺老师的教学链接  本课讲解 类的声明和对象的定义-形式1 #include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( )

C++语言基础 例程 C++的输入输出与流对象

贺老师的教学链接  本课讲解 体会缓冲区 #include <iostream> using namespace std; int main() { int n[5]; for(int i=0; i<5; i++) { cin>>n[i]; cout<<n[i]<<endl; } return 0; }