c++中类的初次接触

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

 1 /*q1.cpp*/
 2 //一个简单的类极其实例化
 3 #include<iostream>
 4 using namespace std;
 5 
 6 class Point{
 7     public:
 8         //设置坐标
 9         void setPoint(int x,int y){
10             xPos = x;
11             yPos = y;
12         }
13         //打印坐标
14         void printPoint(){
15             cout<<"x = "<<xPos<<endl;
16             cout<<"y = "<<yPos<<endl;
17         }
18         //在类外定义函数
19         void foo();
20     private:
21         int xPos;
22         int yPos;
23 };
24 //类外定义的函数
25 void Point::foo(){
26     cout<<"foo是类外定义的函数"<<endl;
27 }
28 
29 int main(){
30     Point point;
31     point.setPoint(10,9);
32     point.printPoint();
33     point.foo();
34     return 0;
35 }
36 
37 
38 
39 /*q2.cpp*/
40 //检测一下public,private,protect的使用权限
41 #include<iostream>
42 using namespace std;
43 class Animal{
44     public:
45         string a = "a dog";
46         void show1(){
47             cout<<a<<endl;
48         }
49     protected:
50         string b = "a snake";
51         void show2(){
52             cout<<b<<endl;
53         }
54     private:
55         string c = "a turtle";
56         void show3(){
57             cout<<c<<endl;
58         }
59 
60 };
61 //只有在类中的public中定义的属性和方法才能从类的外部访问
62 int main(){
63     Animal dog;
64     cout<<dog.a<<endl;
65     dog.show1();
66     return 0;
67 }
原文地址:https://www.cnblogs.com/jeavenwong/p/7512706.html