码迷,mamicode.com
首页 > 编程语言 > 详细

c++入门01

时间:2016-06-18 01:22:03      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

#include "iostream"//预编译(在编译之前进行的操作)命令 文件包含命令
//定义类(抽象数据类型)相当于定义类型 根据类型可以定义变量变量就是对象
//如Data A;A就是对象。Data是数据类型
#include "rect.h"//包含rectangle和Data类的声明定义


void main(){
Data A;
// A.y=5;//保护的数据,外部不可以访问;只有继承Data的子类可以访问
A.s="数数";//以"."型式访问类的成员变量和函数;

std::cout<<A.s<<std::endl;
rectangle rect01(3,4),rect02;
std::cout<<rect01.getlength()<<"====="<<rect02.getlength()<<std::endl;
rect02=rectangle(4,5);//显示调用构造函数
std::cout<<rect01.getlength()<<"====="<<rect02.getlength()<<std::endl;
}

 

其中的#include "rect.h"如下:

class Data{
private:
int x;
protected://专门为继承而设计的权限
int y;
public:
char* s;
public:
Data(){

}
int getx(){
return x;
}
int gety();
};

Data::gety(){
return y;
};

class rectangle{
public:
float length,width;

public:
rectangle(float length=0/*设置默认参数*/,float width=0);//构造函数
float getwidth(void)const;//常量成员函数 不允许修改类中私有成员。也就是不可在这个函数内
//修改 length和width变量的值
float getlength(void)const;

void setLength(float leng);
void setWidth(float wid);
float Perimeter(void)const;//周长
float Area(void)const;//面积
};
// 其中"::"表示范围界定符
rectangle::rectangle(float length,float width){
this->length=length;
this->width=width;
}
float rectangle:: getwidth(void)const{
return width;
}
float rectangle:: getlength(void)const{
return length;
}
void rectangle::setLength(float leng){
this->length=leng;
}
void rectangle::setWidth(float wid){
this->width=wid;
}
float rectangle:: Perimeter(void)const{
return 2*(width+length);
}
float rectangle::Area(void)const{
return width*length;
}

c++入门01

标签:

原文地址:http://www.cnblogs.com/wangshuile/p/5595489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!