c++中类的初次接触
2024-09-03 17:33:32
下面是我写的简单的代码,初次接触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 ;
}
最新文章
- Nodejs - 如何用 eventproxy 模块控制并发
- JDK&;JRE&;JVM
- apache无法正常启动,80端口被占用的解决方法
- jquery中使用event.target的几点
- offsetLeft与style.left区别
- NSUrl 的常见用法
- Struts中的 saveToken的方法
- Redis_基本类型介绍和指令___2
- 【转】Linux命令之查看文件占用空间大小-du,df
- 安卓培训第五天---上传文件SD卡
- hadoop yarn
- 【转】sed 学习笔记
- plsql启动提示监听服务无法连接
- 《java入门第一季》之面向对象综合小案例
- mysqldump备份数据出错
- js获取当前点击元素的索引
- 实现对象属性的lazy-loading(延迟加载)
- java面试题汇总(一)
- DLL Dynamic-Link Library Search Order
- Java API访问ZK的权限控制