C++第3讲:类和对象的介绍,开始上硬菜

    科技2024-06-19  79

    这里定义在类的private下,在类外是没有访问权限的

    #include<iostream> #pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题 using namespace std; struct Hero { char name[10]; int sex; }; //这个一定要写在全局中才能实现对于结构体的打印,但是对于类而言,并不是这样的,还可以写在类里面 void print_info(struct Hero &s) { cout << "name = " << s.name << endl; cout<< "sex = " << s.sex << endl; } class ADVhero { public: char name[10]; int sex; void print_info() { cout << "name = " << name << endl; cout << "sex = " << sex << endl; } }; class Animal { //{}以内是内部 public: //在public下面定义的成员变量和函数,是能够在类的内部和外部都可以进行访问的 char kind[64]; char color[64]; //printAnimal在Animal的内部 void printAnimal() { cout << "kind = " << kind << endl; cout << "color" << color << endl; } void write() { cout << kind << "开始写字了" << endl; } void run() { cout << kind << "开始跑不了" << endl; } //在private下面定义的成员变量和方法只能在类的内部进行访问 private: char kind1[64]; char color1[64]; }; int main(void) { Hero h; strcpy(h.name, "zhx"); h.sex = 1; print_info(h); ADVhero advH; strcpy(advH.name, "zhx"); advH.sex = 2; advH.print_info(); cout << "----------------------" << endl; Animal dog; //这里访问kind和green,因为这两个在类的public下定义的,是可以直接进行访问的 strcpy(dog.kind,"dog"); strcpy(dog.color, "green"); dog.printAnimal(); Animal sheep; strcpy(sheep.kind, "sheep"); strcpy(sheep.color, "white"); sheep.printAnimal(); dog.write(); sheep.run(); system("pause"); return 0; }

    类的封装

    #include<iostream> #pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题 using namespace std; class Hero { int year;//public不写,默认的是private }; //一个结构体默认的访问控制权是public struct Hero2 { int year; void print() { } }; class MyDate { public: void init_date() { cout << "year,month,day" << endl; cin >> year; cin >> month; cin >> day; } void print_date() { cout << year << "年" << month << "月" << day << "日" << endl; } bool is_leap_year() { if( ((year % 4 == 0) && (year % 100 == 0)) || (year % 400 == 0)) { return true; } return false; } int get_year() { return year; } void set_year(int new_year) { year = new_year; } protected://保护控制权限,在类的继承中更private有区别,在单个类中,跟private是一样的 private: int year; int month; int day; }; int main(void) { MyDate my_date; my_date.init_date(); my_date.print_date(); if (my_date.is_leap_year() == true) { cout << "是闰年!" << endl; } else { cout << "不是闰年!" << endl; } //cout<<my_date.year 这个是不行的//所以思考,能否在类的内部定义一个成员函数,将需要访问的成员变量输出来 //getter方法 cout << my_date.get_year() << endl; //setter方法 //my_date.get_year = 2000;显然是不可以的 my_date.set_year(2000); cout << my_date.get_year() << endl; system("pause"); return 0; }

    面向对象案例学习:

    #include<iostream> #pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题 using namespace std; class Rect { }; //周长 double getCircleGirth(double r) { return 2 * 3.14*r; } //面积 double getCircleArea(double r) { return 3.14*r*r; } //用面向对象进行实现 class Circle { public: void setR(double r) { m_r = r; } double getR() { return m_r; } double getGirth() { return 2 * 3.14*m_r; } double getCircleArea() { return 3.14*m_r*m_r; } private: double m_r;//圆的私有成员 }; int main(void) { double r = 10;//半径 double g = 0; double a = 0; g = getCircleGirth(r); a = getCircleArea(r); cout << "面积:" << a << endl; cout << "周长:" <<g << endl; cout << "-------------------------" << endl; Circle circle; circle.setR(10); cout << "圆的半径" << circle.getR() << endl; cout << "面积:" <<circle.getCircleArea() << endl; cout << "周长:" << circle.getGirth() << endl; system("pause"); return 0; }

    多文件使用,将上面的拆开到不同的文件中 主函数:

    #include<iostream> #include"circle.h" using namespace std; int main(void) { Circle circle; circle.setR(10); cout << "圆的半径" << circle.getR() << endl; cout << "面积:" << circle.getCircleArea() << endl; cout << "周长:" << circle.getGirth() << endl; system("pause"); return 0; }

    头文件函数

    #pragma once #ifndef __MAIN_H__ #define __MAIN_H__ #endif //用面向对象进行实现 class Circle { public: void setR(double r);//设置半径 double getR();//得到半径 double getGirth();//得到周长 double getCircleArea();//得到面积 private: double m_r;//圆的私有成员 };

    类定义文件

    #include"circle.h" #pragma warning(disable:4996) //注意这一句的使用,可以解决C语言中出现的一些兼容性问题 void Circle::setR(double r) { m_r = r; } double Circle::getR() { return m_r; } double Circle::getGirth() { return 2 * 3.14*m_r; } double Circle::getCircleArea() { return 3.14*m_r*m_r; }

    由上可知,分开进行书写,一方面方便阅读,一方面还可以进行接口的封装等。

    判断两个立方体是否相等

    #include<iostream> #include"circle.h" using namespace std; //立方体类 class Cube { public: void setABC(int a, int b, int c) { m_a = a; m_b = b; m_c = c; } int getArea() { return (m_a*m_b) * 2 + (m_a*m_c) * 2 + (m_b*m_c) * 2; } int getVolume() { return (m_a*m_b*m_c); } int getA() { return m_a; } int getB() { return m_b; } int getC() { return m_c; } //同类直接无私处 bool judgeCube(Cube &another) //another.getA可以换成another.m_a 。。。。。 { if (m_a == another.getA() && m_b == another.getB() && m_c == another.getC()) { return true; } else { return false; } } private: int m_a; int m_b; int m_c; }; //全局函数(在类外进行定义的) bool judgeCube(Cube &c1, Cube &c2) { if (c1.getA() == c2.getA() && c1.getB() == c2.getB() && c1.getC() == c2.getC()) { return true; } else { return false; } } int main(void) { Cube c1; c1.setABC(10, 20, 30); Cube c2; c2.setABC(10, 20, 30); cout << "c1的面积:" << c1.getArea() << endl; cout << "c1的体积:" << c1.getVolume() << endl; if (judgeCube(c1, c2) == true) cout << "相等" << endl; else { cout << "不相等" << endl; } cout << "---------------------" << endl; if (c1.judgeCube(c2) == true) cout << "相等" << endl; else { cout << "不相等" << endl; } system("pause"); return 0; }

    #include<iostream> using namespace std; //点 class Point { public: void setXY(int x, int y) { m_x = x; m_y = y; } int getX() { return m_x; } int getY() { return m_y; } private: int m_x; int m_y; }; //一个比较真实的情况,我把point放在circle的下面了,然后一直报错,搞了半天没整明白, //最后发现是在circle中使用了point中的数据,但是在使用前却没有定义,需要进行注意。 //圆类 class Circle { public: void setXY(int x, int y) { x0 = x; y0 = y; } void setR(int r) { m_r = r; } //提供一个判断点是否在圆内 //true 在 //false 不在 bool judgePoint(Point &p) { int dd; dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0); if (dd > m_r*m_r) { return false; } else { return true; } } private: int x0; int y0; int m_r; }; int main(void) { Circle c; c.setXY(2, 2); c.setR(4); Point p1; p1.setXY(8, 8); if (c.judgePoint(p1) == true) { cout << "圆的内部" << endl; } else { cout << "圆的外部" << endl; } system("pause"); return 0; }

    这里将上面的圆与点的关系拆分成为多个文件 头文件: circle.h point.h 源文件: circle.cpp point.cpp main.cpp

    **circle.h

    #pragma once #include"point.h" //一个比较真实的情况,我把point放在circle的下面了,然后一直报错,搞了半天没整明白, //最后发现是在circle中使用了point中的数据,但是在使用前却没有定义,需要进行注意。 //圆类 class Circle { public: void setXY(int x, int y); void setR(int r); //提供一个判断点是否在圆内 //true 在 //false 不在 bool judgePoint(Point &p); private: int x0; int y0; int m_r; };

    point.h

    #pragma once //点 class Point { public: void setXY(int x, int y); int getX(); int getY(); private: int m_x; int m_y; };

    circle.cpp

    #include"circle.h" //一个比较真实的情况,我把point放在circle的下面了,然后一直报错,搞了半天没整明白, //最后发现是在circle中使用了point中的数据,但是在使用前却没有定义,需要进行注意。 //圆类 void Circle::setXY(int x, int y) { x0 = x; y0 = y; } void Circle::setR(int r) { m_r = r; } //提供一个判断点是否在圆内 //true 在 //false 不在 bool Circle::judgePoint(Point &p) { int dd; dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0); if (dd > m_r*m_r) { return false; } else { return true; } }

    point.cpp

    #include"point.h" //点 void Point::setXY(int x, int y) { m_x = x; m_y = y; } int Point::getX() { return m_x; } int Point::getY() { return m_y; }

    main.cpp**

    #include<iostream> using namespace std; #include"circle.h" #include"point.h" int main(void) { Circle c; c.setXY(2, 2); c.setR(4); Point p1; p1.setXY(8, 8); if (c.judgePoint(p1) == true) { cout << "圆的内部" << endl; } else { cout << "圆的外部" << endl; } system("pause"); return 0; }

    完整代码是为拆分的时候 C++类相关的学习(本讲完!)

    Processed: 0.018, SQL: 8