下面是我写的简单的代码,初次接触c++中的类,c++真的是博大精深啊,学习c++的路还很长,加油!

 /*q1.cpp*/
//一个简单的类极其实例化
#include<iostream>
using namespace std; class Point{
public:
//设置坐标
void setPoint(int x,int y){
xPos = x;
yPos = y;
}
//打印坐标
void printPoint(){
cout<<"x = "<<xPos<<endl;
cout<<"y = "<<yPos<<endl;
}
//在类外定义函数
void foo();
private:
int xPos;
int yPos;
};
//类外定义的函数
void Point::foo(){
cout<<"foo是类外定义的函数"<<endl;
} int main(){
Point point;
point.setPoint(,);
point.printPoint();
point.foo();
return ;
} /*q2.cpp*/
//检测一下public,private,protect的使用权限
#include<iostream>
using namespace std;
class Animal{
public:
string a = "a dog";
void show1(){
cout<<a<<endl;
}
protected:
string b = "a snake";
void show2(){
cout<<b<<endl;
}
private:
string c = "a turtle";
void show3(){
cout<<c<<endl;
} };
//只有在类中的public中定义的属性和方法才能从类的外部访问
int main(){
Animal dog;
cout<<dog.a<<endl;
dog.show1();
return ;
}

最新文章

  1. Nodejs - 如何用 eventproxy 模块控制并发
  2. JDK&amp;JRE&amp;JVM
  3. apache无法正常启动,80端口被占用的解决方法
  4. jquery中使用event.target的几点
  5. offsetLeft与style.left区别
  6. NSUrl 的常见用法
  7. Struts中的 saveToken的方法
  8. Redis_基本类型介绍和指令___2
  9. 【转】Linux命令之查看文件占用空间大小-du,df
  10. 安卓培训第五天---上传文件SD卡
  11. hadoop yarn
  12. 【转】sed 学习笔记
  13. plsql启动提示监听服务无法连接
  14. 《java入门第一季》之面向对象综合小案例
  15. mysqldump备份数据出错
  16. js获取当前点击元素的索引
  17. 实现对象属性的lazy-loading(延迟加载)
  18. java面试题汇总(一)
  19. DLL Dynamic-Link Library Search Order
  20. Java API访问ZK的权限控制

热门文章

  1. 学习BM算法
  2. Java String类源码
  3. 搭建Leanote网络云笔记
  4. VS 之github
  5. php 如何将image图片转化为字符串(GD库操作及imagick两种实现方式)
  6. vue组件常用传值
  7. linux MD5使用
  8. 【Struts2】 国际化
  9. linux入门常用指令2.安装nginx
  10. web开发:Bootstrap应用及内存管理