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(height*width*length);
}

int main( )
{
    Box a[3]=
    {
        Box(),
        Box(15,18),
        Box(16,20,26)
    };
    cout<<"volume of a[0] is "<<a[0].volume( )<<endl;
    cout<<"volume of a[1] is "<<a[1].volume( )<<endl;
    cout<<"volume of a[2] is "<<a[2].volume( )<<endl;
    return 0;
}
时间: 2024-07-31 08:07:45

C++语言基础 例程 对象数组的相关文章

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<<"========调用构造函数==

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 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++语言基础 例程 类和对象的简单应用举例

贺老师的教学链接  本课讲解 实例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; }

C++语言基础 例程 基于对象的程序的执行过程

贺老师的教学链接  本课讲解 #include <iostream> #include <cstring> using namespace std; class Student { public: void set_data(int n, char *p,char s); void display( ); private: int num; char name[20]; char sex; }; void Student::set_data(int n, char *p,char

C++语言基础 例程 文本文件的读写

贺老师的教学链接  本课讲解 示例:将数据写入ASCII文件 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10]; ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat" if(!outfile) //如果打开失败