1 Star 0 Fork 0

Sjianzhao / C++学习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
基本类.cpp 16.92 KB
一键复制 编辑 原始数据 按行查看 历史
Sjianzhao 提交于 2021-04-06 18:34 . update
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
// #include<iostream>
// #include<string>
// #include<stdio.h>
// #include<stdlib.h>
// using namespace std;
// //类是一种ADT,因此不占内存,所以 不能在类里面进行初始化
// typedef class jz_MyClass
// {
// private:
// int hour;
// int min;
// int sec;
// public:
// jz_MyClass() //无参的构造函数,函数名和类名相同,用于类的初始化
// {
// hour = 0; //无参的构造函数 初始化方式
// min = 0;
// sec = 0;
// }
// int jz_input_data();
// int jz_datarchu();
// }jz_st;
// int jz_st::jz_input_data()
// {
// cin>>hour>>min>>sec;
// return 0;
// }
// int jz_st::jz_datarchu()
// {
// cout << hour << ":" << min << ":" << sec << endl;
// return 0;
// }
// //构造函数使用
// // 构造函数的例外一种用法
// typedef class jz_std2
// {
// private:
// int lenth;
// int wid;
// int hei;
// public:
// jz_std2(int a,int b,int c); //类里面的函数不带类型的都是构造函数 ,,,,类内声明,可不带参
// int Volume();
// }jz_st2;
// jz_std2::jz_std2(int a, int b, int c)
// {
// lenth = a;
// wid = b;
// hei = c;
// }
// int jz_std2::Volume()
// {
// return lenth * wid * hei;
// }
// #define gouzao_1 0
// #define gouzao_2 0
// #define gouzao_3 0
// #define gouzao_4 0
// typedef class jz_std3
// {
// private:
// int le;
// int we;
// int he;
// public: //构造函数不能被调用,只能在定义类变量是进行赋值
// #if gouzao_1
// jz_st3() //无参的构造函数
// {
// le = 4; we = 5; he = 6; //无参的构造函数只能在 类内部 写 初始化函数
// }
// #endif
// #if gouzao_2
// jz_std3(int, int, int); //带参的构造函数
// #endif
// #if gouzao_3
// jz_std3(int l, int w, int h) :le(l), we(w), he(h) { } //有参的构造函数,用初始化表进行初始化
// #endif
// #if gouzao_4
// jz_std3(int l = 10, int w = 10, int h = 10);
// #endif
// int volum();
// }jz_st3;
// #if (gouzao_2||gouzao_4)
// jz_st3::jz_std3(int l,int w,int h)
// {
// le = l; we = w;he = h;
// }
// #endif
// int jz_st3::volum()
// {
// return le * we * he ;
// }
// //析构函数使用
// //顺序:创建类类型变量----》构造函数----》使用类内部变量----》析构函数
// //先构造的后析构,后构造的先析构
// typedef class jz_std4
// {
// private:
// int num;
// string name;
// int sex;
// public:
// jz_std4(int, string, int);
// ~jz_std4()
// {
// cout << "析构函数" << endl; //没有打印出来
// }
// int display();
// }jz_st4;
// jz_st4::jz_std4(int a, string b, int c)
// {
// num = a;
// name = b;
// sex = c;
// cout << "初始化成功" << endl; //没有打印出来
// }
// int jz_st4::display(void)
// {
// cout << "学号 :" << num << endl << "姓名 :" << name << endl << "分数 :" << sex << endl;
// return 0;
// }
// //对象数组
// typedef class jz_std5
// {
// private:
// int le;
// int we;
// int he;
// public:
// jz_std5(int, int, int);
// ~jz_std5();
// int volue();
// }jz_st5;
// jz_st5::jz_std5(int a,int b,int c)
// {
// le = a;
// we = b;
// he = c;
// }
// jz_st5::~jz_std5()
// {
// ;
// }
// int jz_st5::volue(void)
// {
// cout << "ti ji = " << (he * we * le) << endl;
// return 0;
// }
// //指针的使用 -----> 类的函数指针
// typedef class jz_std6
// {
// private:
// int h;
// int m;
// int s;
// public:
// jz_std6(int, int, int);
// ~jz_std6();
// int volue();
// }jz_st6;
// jz_st6::jz_std6(int a, int b, int c)
// {
// h = a;
// m = b;
// s = c;
// }
// jz_st6::~jz_std6()
// {
// ;
// }
// int jz_st6::volue(void)
// {
// cout << h << ":" << m << ":" << s << ":" << endl;
// return 0;
// }
// //对象的引用,对象的引用中,引用变量不能是私有的
// //第一章学了变量的引用,&b = a ;
// typedef class jz_std7
// {
// public:
// int hour;
// int min;
// int sec;
// jz_std7(int,int,int);
// ~jz_std7();
// int out_put();
// }jz_st7;
// jz_st7::jz_std7(int a,int b,int c)
// {
// hour = a;
// min = b;
// sec = c;
// }
// jz_st7::~jz_std7()
// {
// ;
// }
// void fun(jz_st7 &num) //引用对象的函数也不是类成员函数
// {
// num.hour = 11;
// num.min = 22;
// }
// int jz_st7::out_put()
// {
// cout << hour << ":" << min << ":" << sec << endl;
// return 0;
// }
// //对象的赋值只能在同一个类中进行
// // 把对象 A的值给对象 B -------> B = A ;(A和B是同一个类的对象)
// typedef class jz_std8
// {
// private:
// int num;
// int sum;
// int aum;
// public:
// jz_std8(int h, int l, int w): num(h), sum(l), aum(w) {}
// ~jz_std8();
// int out_put();
// }jz_st8;
// jz_st8::~jz_std8()
// {
// ;
// }
// int jz_st8::out_put()
// {
// cout << num << ":" << sum << ":" << aum << endl;
// return 0;
// }
// //类变量的部分初始化 和 静态变量的使用
// //静态变量是类的一部分,不是对象的一部分,
// //静态变量是同一个类的不同对象共同使用的。
// typedef class jz_std9
// {
// private:
// int num;
// int age;
// int soure;
// static int sum;
// static int count;
// public:
// jz_std9(int a, int b, int c) :num(a), age(b), soure(c) {};
// ~jz_std9();
// int out_even();
// static int out_sum(); //静态成员最好使用静态成员函数进行引用,当然其他的成员函数也可以引用静态变量
// }jz_st9;
// int jz_st9::sum = 0; //静态变量的初始化
// int jz_st9::count = 0;
// jz_st9::~jz_std9()
// {
// ;
// }
// int jz_st9::out_even()
// {
// sum += soure;
// count++;
// return 0;
// }
// int jz_st9::out_sum()
// {
// return sum / count;
// }
// //友元函数
// //不管在哪定义的函数,只要在本类中声明了友元函数,就可以访问本类的私有变量了
// class jz_std11;
// typedef class jz_std10
// {
// private :
// int num;
// int text;
// int mun;
// public :
// jz_std10(int a, int b, int c) :num(a), text(b), mun(c) {};
// ~jz_std10();
// // friend int jz_std11::display(jz_std10 &); //进行友元声明,声明在其他类定义,会用到本类的成员,形参就是传入本类的类变量
// }jz_st10;
// jz_st10::~jz_std10()
// {
// ;
// }
// typedef class jz_std11
// {
// private:
// int num1;
// int text1;
// int mun1;
// public:
// jz_std11(int a, int b, int c) :num1(a), text1(b), mun1(c) {};
// ~jz_std11();
// int display(jz_std10 &);
// }jz_st11;
// jz_st11::~jz_std11()
// {
// ;
// }
// int jz_std11::display(jz_std10 &t)
// {
// cout << num1 << "/" << text1 << "/" << mun1 << endl;
// // cout << t.num << ":" << t.text << ":" << t.mun << ":" << endl;
// return 0;
// }
// //所谓对象,就是定义的类类型变量,如jz_st num; num就是一个对象
// int main(void)
// {
// //构造函数的使用
// #if 0
// jz_st p1;
// p1.jz_input_data(); //输入:23 45 12
// p1.jz_datarchu(); //输出:23 45 12
// jz_st p2;
// p2.jz_datarchu(); //输出:00 00 00 因为输入的是p1作用域的值,所以p2还是没变
// #endif
// #if 0
// jz_st2 q1(4,5,6);
// cout << q1.Volume() << endl;
// jz_st2 q2(4, 3, 2);
// cout << q2.Volume() << endl;
// #endif
// #if gouzao_1
// jz_st3 point;
// cout << point.volum() << endl;
// #endif
// #if gouzao_2
// jz_st3 point(7,8,9);
// cout << point.volum() << endl;
// #endif
// #if gouzao_3
// jz_st3 point(11,11,11);
// cout << point.volum() << endl;
// #endif
// #if gouzao_4
// jz_st3 point;
// cout << point.volum() << endl;
// #endif
// //析构函数的使用
// /*
// jz_st4 student(1001, "sjz", 94);
// student.display();
// */
// // //内存测试
// //jz_st5 point2; // 8 字节 char shout int
// //cout << sizeof(jz_st5) << endl;
// //string a = "asddasdasdasda";
// //cout << sizeof(string) << endl << sizeof(a)<<endl; //都是28
// // //类似于jz_st5 p1 = {2,3,4}; p2 = {5,5,5}; p3 = {10,10,10};
// // //类似于结构体数组 std table[3];
// //jz_st5 point3[3] = {\
// // {2,4,5},{5,5,5},{10,10,10}\
// //};
// //point3[0].volue();
// //point3[1].volue();
// //point3[2].volue();
// //类指针,有类指针,类函数指针
// #if 1
// jz_st6 table[3] = {
// {12,23,34},{8,30,00},{23,32,21}
// };
// jz_st6 *p = NULL; //类指针,指向一个类的
// int (jz_st6::*q)(); //类函数指针,指向一个函数的首地址的
// table[0].volue();
// table[1].volue();
// table[2].volue();
// p = &table[0];
// if (NULL == p)
// {
// return 0;
// }
// (*p).volue(); //p->volue();
// q = &jz_st6::volue; //*q就代表类里面的函数volue了
// (table[2].*q)();
// #elif
// jz_st6 *prt[2] = {
// NULL,NULL
// };
// jz_st6 pin(1, 2, 3), pin2(4, 5, 6);
// prt[0] = &pin;
// (*prt[0]).volue();
// int(jz_st6::*p)();
// p = &jz_st6::volue;
// (pin2.*p)();
// #endif
// //对象的引用,只能定义外部函数引用,因此不能用于应用类的私有成员
// jz_st7 rst = { 8,9,10 };
// rst.out_put();
// fun(rst); //类的引用 fun是外部函数
// rst.out_put();
// //动态分配类内存
// jz_st7 *pp = NULL; //定义一个类指针
// pp = new jz_st7(2,3,4); //指向一块开辟的动态类内存空间,内存分配
// if (NULL == pp)
// {
// return 0;
// }
// (*pp).out_put(); //输出分配的内存的值
// delete(pp); //释放内存
// pp = NULL;
// cout << pp << endl;
// jz_st8 box[2] = {
// { 3, 4, 5 },{9,8,7}
// };
// box[0].out_put();
// box[0] = box[1]; //同一个类的对象进行赋值
// box[0].out_put();
// //部分成员的初始化
// jz_st9 stu[5] = {
// {1001,21,78},{1002,22,81},{1003,23,67},{1004,24,79}\
// ,{1005,25,81}
// };
// int n;
// cout << "请输入学生个数 : " << endl;
// cin >> n;
// for (int i = 0; i < n; i++)
// {
// stu[i].out_even(); //同一个类的不同对象,公用 静态成员
// }
// cout << "average = " << stu[0].out_sum() << endl; //输出类里面的静态成员,只要是在同一个类,任何一个对象都可以进行输出
// jz_st10 pwm = { 20,12,24 };
// jz_st11 pwm1 = { 20,5,30 };
// pwm1.display(pwm);
// system("pause");
// return 0;
// }
// typedef class jz_std1
// {
// private:
// int manth;
// int day;
// int year;
// public:
// // jz_std1(int = 1, int = 1, int = 2005);
// jz_std1(int a, int b, int c) :manth(a), day(b), year(c) { ; } //构造函数的重载
// jz_std1(int, int); //构造函数的重载
// jz_std1(int); //构造函数的重载
// jz_std1(); //构造函数的重载
// void display();
// }jz_st1;
// jz_st1::jz_std1(int a, int b) :manth(a), day(b)
// {
// year = 2020;
// }
// jz_st1::jz_std1(int a) : manth(a)
// {
// day = 12;
// year = 2020;
// }
// jz_st1::jz_std1()
// {
// manth = 12;
// day = 12;
// year = 2020;
// }
// void jz_st1::display()
// {
// cout << year << ":" << manth << ":" << day << ":" << endl;
// }
// typedef class jz_std2
// {
// private:
// int num;
// int sec;
// public:
// jz_std2(int a, int b) :num(a), sec(b) { ; }
// int out_put();
// }jz_st2;
// int jz_st2::out_put()
// {
// cout << "学号 :" << num << endl << "成绩 :" << sec << endl;
// return 0;
// }
// typedef class jz_std3
// {
// private:
// int num;
// int sec;
// public:
// jz_std3(int a, int b) :num(a), sec(b) { ; }
// int out_max(jz_std3 *p);
// }jz_st3;
// int jz_st3::out_max(jz_std3 *p)
// {
// int i = 0;
// int max = 0;
// for (i = 0; i < 5; i++)
// {
// if (((p + i)->sec) > max)
// {
// max = (*(p + i)).sec;
// }
// }
// cout << "max sec :" << max << endl;
// return max;
// }
// typedef class jz_std4
// {
// private:
// int num;
// float score;
// public:
// jz_std4(int a, float b) :num(a), score(b) { ; }
// void change(int a, float b) { num = a; score = b; }
// void display() { cout << num << " " << score << endl; }
// void fun(jz_std4 & p);
// }jz_st4;
// void jz_st4::fun(jz_st4 & p)
// {
// change(103, 99.5);
// display();
// }
// typedef class jz_std5
// {
// private:
// int num;
// int quantity;
// float price;
// static float sum;
// static float aver;
// public:
// jz_std5(int a, int b, float c) :num(a), quantity(b), price(c) { ; }
// ~jz_std5();
// void out_sum(jz_std5 *p);
// void out_aver(jz_std5 *p);
// }jz_st5;
// jz_st5::~jz_std5()
// {
// ;
// }
// void jz_st5::out_sum(jz_std5 *p)
// {
// int i;
// for (i = 0; i < 3; i++)
// {
// sum += (float)(((*(p + i)).quantity) * ((*(p + i)).price));
// }
// cout << "zong xiao shou kuan :" << sum << endl;
// }
// void jz_st5::out_aver(jz_std5 *p)
// {
// int sum_quan = 0; int i;
// for (i = 0; i < 3; i++)
// {
// sum_quan += (*(p + i)).quantity;
// }
// cout << "zong xiao liang : " << sum_quan << endl;
// aver = (float)(sum / sum_quan);
// cout << "ping jun jia ge : " << aver;
// }
// int main()
// {
// ///////////////////////////////////////
// jz_st1 t1[4] = {
// { 11,11,11 },{ 11,11 },{ 11 }
// };
// t1[0].display();
// t1[1].display();
// t1[2].display();
// t1[3].display();
// /////////////////////////////////////
// jz_st2 stu2[5] = {
// { 1001,75 },{ 1002,78 },{ 1003,64 },{ 1004,86 },{ 1005,97 }
// };
// jz_st2 *p2 = NULL;
// p2 = stu2;
// for (int i = 0; i < 5; i += 2)
// {
// (p2 + i)->out_put();
// }
// ///////////////////////////////////////////
// jz_st3 stu3[5] = {
// { 1001,75 },{ 1002,78 },{ 1003,99 },{ 1004,86 },{ 1005,97 }
// };
// stu3[0].out_max(stu3);
// ////////////////////////////////////////////
// jz_st4 stud(101, 78.5);
// jz_st4 *p4 = NULL;
// p4 = &stud;
// (*p4).display();
// (*p4).change(102, 80.5);
// (*p4).display();
// stud.fun(stud);
// jz_st5 num[3] = {
// { 101,5,23.5F },{ 102,12,24.56F },{ 103,100,21.5F }
// };
// num[1].out_sum(num);
// num[1].out_aver(num);
// system("pause");
// return 0;
// }
C++
1
https://gitee.com/Sjianzhao/Cplusplus.git
git@gitee.com:Sjianzhao/Cplusplus.git
Sjianzhao
Cplusplus
C++学习
master

搜索帮助