指针与对象,指针与对象数组的联系与区别
来源:互联网 发布:什么是网络安全策略 编辑:程序博客网 时间:2024/06/01 21:01
#include <iostream>using namespace std;class Recta{public: Recta(int len=10,int wid=10) { length=len; width=wid; } int getArea() { return (length*width); } void disp() { cout<<"长是:"<<length<<" "<<"高是:"<<width<<endl; cout<<"面积:"<<getArea()<<endl;//rec[i].getArea() 是错的,类是整体,不知道主函数里有什么 }private: int length,width;};int main(){ Recta rec[3]={ Recta(15,20), Recta(5,6), Recta(30,40) }; for(int i=0;i<3;i++) { rec[i].disp(); } return 0;}//=====================================================================================#include <iostream>using namespace std;class Date{public: Date(int y,int m,int d); void setDate(int y,int m,int d); void show();private: int year; int month; int day;};Date::Date(int y,int m,int d){ year=y; month=m; day=d;}inline void Date::setDate(int y,int m,int d){ year=y; month=m; day=d;}void Date::show(){ cout<<year<<"."<<month<<"."<<day<<endl;}int main(){ Date date(2013,2,25); date.show(); Date *dp; dp=&date; //这的date是对象,不是指针,所以要用取地址符 dp->setDate(2012,11,16); dp->show(); return 0;}//=============================================================#include <iostream>using namespace std;class Date{public: Date(int y=2012,int m=7,int d=20); void setDate(int y,int m,int d); void show();private: int year; int month; int day;};Date::Date(int y,int m,int d){ year=y; month=m; day=d;}inline void Date::setDate(int y,int m,int d){ year=y; month=m; day=d;}void Date::show(){ cout<<year<<"."<<month<<"."<<day<<endl;}int main(){ Date date[2]; Date *dp; date[0].setDate(2012,11,16); date[1].setDate(2013,2,25); dp=date; //date[]是对象数组,date是起始地址,即指针,所以不用取地址符号。当把对象数组中的第i个赋给指针时,则用dp=&date[i] dp->show(); dp++; dp->show(); return 0;}
- 指针与对象,指针与对象数组的联系与区别
- 指针与数组的区别与联系
- 指针与数组的联系与区别
- 数组与指针的区别与联系
- 指针与数组的区别和联系
- 指针与数组的联系和区别
- 指针与数组的区别和联系
- 指针与数组的区别和联系
- 指针与数组的区别和联系
- 指针与数组的区别和联系
- 数组和指针的区别与联系
- 数组与对象的区别与联系
- 指针与数组的区别与联系[完整版][转帖]
- 二级指针与数组地址的区别与联系
- 字符数组与字符指针的区别与联系
- 浅析数组与指针的联系与区别1
- 浅析数组与指针的联系与区别2
- char数组与char指针的区别与联系
- VSS的配置和使用
- 世界最小超声波传感器问世
- LINUX SHELL 学习摘要
- Visual C#常用函数和方法集汇总 (1)
- hdu1045
- 指针与对象,指针与对象数组的联系与区别
- 收藏夹1
- jsp中session的讲解
- C#时间日期操作
- cat一个大的非文本文件后,屏幕字符变乱
- 工作积累之在IIS上配置ASP.NET
- Object 学习笔记1
- 上班一周感
- 为读者做好计算