类型转换函数与构造转换函数
#include<iostream> class fushu { public: explicit fushu(int num)//避免隐式转换,引发歧义 { x = num; y = num; } void print() { std::cout << x << "+" << y << "i" << std::endl; } operator int(); //不支持友元,仅仅支持成员函数 operator double() { return (double)(x + y); } protected: private: int x; int y; }; fushu::operator int()//类类之间的转换函数 { return x *y; } //转换有赋值,()构造 void main3() { int num(100.9); fushu fushu1(num);//构造函数 fushu1.print(); int data = (int)fushu1 + 10;//类型转换可以把一个自定义类型当作基本数据类型来计算 std::cout << data << std::endl; std::cin.get(); } void main2() { int num ( 100.9); fushu fushu1(num);//构造函数 fushu1.print(); //int data (fushu1);//转换 //int data = fushu1; //int data = (int)fushu1; //int data = int (fushu1); //int data ( int(fushu1)); //std::cout << data << std::endl; std::cin.get(); } void main1() { int num (10.8);//基本数据类型 //隐式转换与显式转换,是否带有类型转换符 //fushu fushu1 = (fushu)10.8; //fushu fushu1 = static_cast<fushu>(10.8); fushu fushu1 =(fushu)10;//构造函数 //int numA = fushu1; fushu1.print(); std::cin.get(); }
类与类之间的转换
#include <iostream> class mianji { public: friend class fushu; mianji() { this->cx = 0; this->cy = 0; } void setxy(int a,int b) { this->cx = a; this->cy = b; } protected: private: int cx; int cy; }; class fushu { public: friend class mianji;//友元可以访问私有变量 fushu(mianji mianji1) { this->x = mianji1.cx; this->y = mianji1.cy; } void print() { std::cout << x << "+" << y << "i" << std::endl; } operator mianji() { mianji temp; temp.cx = x; temp.cy = y; return temp; } protected: private: int x; int y; }; void main() { mianji mianji1; //fushu fushu1 =(fushu) mianji1; fushu fushu1(mianji1);//构造函数 //= static_cast<fushu>(mianji1);//构造函数进行转换 mianji mianji2; mianji2.setxy(10, 20); fushu1 = mianji2; fushu1.print(); std::cin.get(); }
类的继承
类与类之间三种关系:包含,调用,继承。
#include "mainwindow.h" #include <QApplication> #include<stdlib.h> //第一种关系,某一个类作为某一个类的部件 class mywindow { public: MainWindow w; }; class show { public: void showwindow(MainWindow & w )//部分的使用一个类,调用一个类 { w.show(); } }; class newwindow:public MainWindow { public: void run(char *str) { system(str); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); newwindow new1; new1.show(); new1.run("calc"); return a.exec(); } int mainA(int argc, char *argv[]) { QApplication a(argc, argv); mywindow my1; // my1.w.show(); show show1; show1.showwindow(my1.w); return a.exec(); }
#pragma once #include<iostream> class coder { private: char *str; public: coder(); ~coder(); void girlfriend(); void coding(); protected://不允许外部访问,允许子类内部访问 int num; }; #include "coder.h" coder::coder() { std::cout << "coder create" << std::endl; str = "锄禾日当午"; num = 100; } coder::~coder() { std::cout << "coder delete" << std::endl; } void coder::girlfriend() { std::cout << "一般都会写代码,可以创建一个对象,可是没有对象" << std::endl; } void coder::coding() { std::cout << "加班加点熬夜" << std::endl; } #pragma once #include "coder.h" //父类的私有成员,不行 //父类的保护成员,公有成员内部可以访问 //保护成员,公有成员都会变成保护成员 class ccoder:protected coder { public: ccoder(); ~ccoder(); }; #include "ccoder.h" ccoder::ccoder() { } ccoder::~ccoder() { } #pragma once #include "coder.h" //父类的私有成员,不行 //父类的保护成员,公有成员都可以 //公有继承,不影响保护成员,公有成员属性变化 class cppcoder : public coder { public: cppcoder(); ~cppcoder(); void coding(); void ui(); }; #include "cppcoder.h" cppcoder::cppcoder() { std::cout << "cpp coder create" << std::endl; } cppcoder::~cppcoder() { std::cout << " cpp coder delete" << std::endl; } void cppcoder::ui() { std::cout << "QT真蛋疼" << std::endl; //子类的内部可以访问 std::cout << (this->coder::num) << std::endl; } void cppcoder::coding() { std::cout << "CPP真蛋疼" << std::endl; } #pragma once #include "coder.h" //父类的私有成员,不行 //父类的保护成员,公有成员内部可以访问 //私有继承,公有成员,保护成员都变成私有成员 class javacoder : private coder { public: javacoder(); ~javacoder(); }; #include "javacoder.h" javacoder::javacoder() { this->num = 100; } javacoder::~javacoder() { } //main.cpp #include<iostream> #include "coder.h" #include "cppcoder.h" #include "javacoder.h" void main() { javacoder *pjava = new javacoder; //pjava->num = 100; } void main3() { cppcoder *ptansheng = new cppcoder; coder *pcode = new coder; //protected不允许外部访问,允许子类内部访问 ptansheng->ui(); std::cin.get(); } void main2() { coder *pcode = new cppcoder;//父类的指针,接受子类指针的地址 pcode->coding(); cppcoder *pcppcoder = reinterpret_cast<cppcoder *>(pcode); pcppcoder->coding(); std::cout << typeid(pcode).name() << std::endl; std::cout << typeid(pcppcoder).name() << std::endl; std::cin.get(); } void main1() { cppcoder *ptansheng = new cppcoder; ptansheng->girlfriend(); ptansheng->ui(); //子类与父类重名,覆盖父类 ptansheng->coding(); //每一个子类都会生成一个默认的父类对象 //调用父类的同名方法 ptansheng->coder::coding(); //父类的私有无法使用 delete ptansheng; std::cin.get(); }
类的继承以及区别
#include<iostream> class father { public: int num; void print() { std::cout << num << std::endl; } father() { num = 99; } }; class son:public father { public: int num; void print() { std::cout << num << std::endl; } son() //子类覆盖父类 { num = 89; } }; void main() { son *pson = new son; pson->print(); pson->father::print(); father *p = reinterpret_cast<father *>( pson); p->print(); std::cin.get(); }
继承静态成员与静态函数
#include<iostream> class myclass { public: int data; static int num;//声明静态变量存在 myclass() { num++;//共享,统计对象的数目 } static void print() { //this->data;//静态函数无法使用this指针 //data = 10; std::cout << num << std::endl; } }; int myclass::num = 0;//静态变量初始化 //private私有继承,无法传承到下一代 class ziclass:protected myclass { void run() { this->print(); this->num; } }; class sunclass :protected ziclass { void goandrun() { this->ziclass::myclass::print(); } }; void main() { ziclass *p = new ziclass; ziclass z1; sunclass *ps = new sunclass; //int a; //p->num; //p->print(); p->myclass::num; p->myclass::print(); //ps->print(); //ps->ziclass::myclass::print(); std::cin.get(); }
继承实现代码重用
基类的初始化
#include<iostream> class myclass { public: myclass() :x(0) { //x = 0; std::cout << "myclass init without num" << std::endl; } myclass(int num) :x(num) { //x = num; std::cout << "myclass init with num" << std::endl; } protected: private: int x; }; class myziclass:public myclass { public: myziclass() { std::cout << "myziclass init without num" << std::endl; } myziclass(int num) :myclass(num), x(num + 1), y(num+2) { std::cout << "myziclass init with num" << std::endl; } int x; int y; }; void main() { //指定构造函数 myziclass *p = new myziclass(10); std::cin.get(); }
立方体
#include <iostream> #include<math.h> class dian { public: friend class xian; dian(int a, int b, int c) :x(a), y(b), z(c) { } void print() { std::cout << "x=" << x << ",y=" << y << ",z=" << z << std::endl; } private: int x; int y; int z; }; //继承没有意义,包含 class xian { public: xian(dian dianx, dian diany) :dian1(dianx), dian2(diany) { } double getlength() { double length = 0; length = sqrt((dian1.x - dian2.x)*(dian1.x - dian2.x) + (dian1.y - dian2.y)*(dian1.y - dian2.y) + (dian1.z - dian2.z)*(dian1.z - dian2.z)); return length; } dian dian1; dian dian2; protected: private: }; class yuan :public xian { public: yuan(dian dianx, dian diany) :xian(dianx,diany) { } double getmianji() { return 3.1415926* (this->getlength())*(this->getlength()); } double zhouchang() { return 3.1415926* 2*(this->getlength()); } }; class qiu :public yuan { public: qiu(dian dian1,dian dian2) :yuan(dian1,dian2) { } double getmianji() { return 3.1415926* (this->getlength())*(this->getlength()) * 4; } double gettiji() { return 4 / 3.0*3.1415926* (this->getlength())* (this->getlength())* (this->getlength()); } }; void main() { dian dian1(0, 0, 1); dian dian2(0, 0, 6); dian1.print(); dian2.print(); xian xian1(dian1, dian2); std::cout << xian1.getlength() << std::endl; yuan yuan1(dian1, dian2); std::cout << yuan1.getmianji() << std::endl; std::cout << yuan1.zhouchang() << std::endl; qiu qiu1(dian1, dian2); std::cout << qiu1.gettiji() << std::endl; std::cout << qiu1.getmianji() << std::endl; std::cin.get(); }
单继承QT案例
#include <QApplication> #include<QLabel> #include<stdlib.h> class mylable:public QLabel { public: mylable( char *str):QLabel(str) { } void run (char *str) { system(str); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); // QLabel *plabel =new QLabel("ABCDEF"); // plabel->show(); mylable my1("12345ABC"); my1.show(); my1.run("notepad"); return a.exec(); }
多继承简介以及实战
#include "mainwindow.h" #include <QApplication> #include<QPushButton> #include<QLabel> class zajiao :public MainWindow,public QLabel,public QPushButton { public: zajiao(char *str):QLabel(str),QPushButton(str) { this->MainWindow::setWindowTitle(str); } ~zajiao() { } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); zajiao zajiao1("ABCDEF12345"); zajiao1.QPushButton::show(); zajiao1.QPushButton::move(0,0); zajiao1.QLabel::show(); zajiao1.MainWindow::show(); return a.exec(); } int mainA(int argc, char *argv[]) { QApplication a(argc, argv); //MainWindow w; //w.show(); // w.setWindowTitle("ABCD"); // QPushButton *p1=new QPushButton("ABC"); // p1->show(); return a.exec(); }
#include<iostream> #include<stdlib.h> class A { }; class B { }; class myclass1 { public: void run(char *str) { system(str); } myclass1() { std::cout << "myclass1 is create" << std::endl; } ~myclass1() { std::cout << "myclass1 is delete" << std::endl; } }; class myclass2 { public: int add(int a, int b) { return a + b; } myclass2() { std::cout << "myclass2 is create" << std::endl; } ~myclass2() { std::cout << "myclass2 is delete" << std::endl; } }; class myclass:public myclass1,public myclass2,public A,public B { public: void print(char *str) { std::cout << str << std::endl; } myclass() { std::cout << "myclass is create" << std::endl; } ~myclass() { std::cout << "myclass is delete" << std::endl; } }; void main2() { myclass *pmy1 = new myclass; delete pmy1; std::cin.get(); } void main1() { myclass my1; my1.run("tasklist"); my1.myclass1::run("ipconfig"); std::cout << my1.add(10, 20) << std::endl; std::cout << my1.myclass2::add(19,20) << std::endl; my1.print("12345"); std::cin.get(); }
虚基类
虚基类的作用
当一个基类被声明为虚基类后,即使它成为了多继承链路上的公共基类,最后的派生类中也只有它的一个备份。
#include <iostream> class obj { public: int num; obj(int data) :num(data) { std::cout << "obj create\n"; } obj() { num = 0; std::cout << "obj create\n"; } ~obj() { std::cout << "obj delete\n"; } }; class Aobj : virtual public obj { public: Aobj(int data) :obj(data) { std::cout << "Aobj create\n"; } ~Aobj() { std::cout << "Aobj delete\n"; } }; class Bobj : virtual public obj { public: Bobj(int data) :obj(data) { std::cout << "Bobj create\n"; } ~Bobj() { std::cout << "Bobj delete\n"; } }; class ABobj :public Aobj, public Bobj { public: ABobj(int x, int y) :Aobj(x), Bobj(y) { std::cout << "ABobj create\n"; } ABobj(int z) :Aobj(z), Bobj(z) { std::cout << "ABobj create\n"; } ~ABobj() { std::cout << "ABobj delete\n"; } }; void main() { ABobj *p = new ABobj(10); //std::cout << p->num; std::cout << p->Aobj::obj::num << "\n"; std::cout << p->Bobj::obj::num << "\n"; delete p; std::cin.get(); }
继承以及作业安排
#include <iostream> #include <amp.h> #include<thread> #include<vector> using namespace std; using namespace std::this_thread; using namespace concurrency; void run() { int v[11] = { 'G', 'd', 'k', 'k', 'n', 31, 'v', 'n', 'q', 'k', 'c' }; array_view<int> av(11, v); parallel_for_each(av.extent, [=](index<1> idx) restrict(amp) { av[idx] += 1; }); for (unsigned int i = 0; i < 11; i++) std::cout << static_cast<char>(av[i]); std::cin.get(); } int main1() { vector<thread*> threads; for (unsigned int i = 0; i < 5; i++) { threads.push_back(new thread(run)); } for (auto x : threads) { x->join(); } std::cin.get(); return 0; }
Gpu编程
#include <iostream> #include <amp.h> #include <WinBase.h> #define COUNT 10000 float nickName_GPU[COUNT]; float nickName_CPU[COUNT]; double rungpu(int num)restrict(amp) { double temp = 0; for (int i = 0; i < num; i++) { temp += i; } return temp; } double runcpu(int num)restrict(cpu) { double temp = 0; for (int i = 0; i < num; i++) { temp += i; } return temp; } double runcpugpu(int num) restrict(amp, cpu) { double temp = 0; for (int i = 0; i < num; i++) { temp += i; } return temp; } int main(void) { LARGE_INTEGER freq; LARGE_INTEGER strt; LARGE_INTEGER ed; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&strt); concurrency::array_view<float> myView(COUNT, nickName_GPU); //将数据打入显存 concurrency::parallel_for_each(myView.extent, [=](concurrency::index<1> idx) restrict(amp) { for (int i = 0; i < 100000; i++) { myView[idx] = (myView[idx] + 0.1f) / 2.3f; } }); myView.synchronize();//显式等待GPU计算完成并将数据打回内存 QueryPerformanceCounter(&ed); printf("GPU耗时: %d 毫秒\r\n", (ed.QuadPart - strt.QuadPart) * 1000 / freq.QuadPart); QueryPerformanceCounter(&strt); for (int idx = 0; idx < COUNT; idx++) { for (int i = 0; i < 100000; i++) { nickName_CPU[idx] = (nickName_CPU[idx] + 0.1f) / 2.3f; } } QueryPerformanceCounter(&ed); printf("CPU耗时: %d 毫秒\r\n", (ed.QuadPart - strt.QuadPart) * 1000 / freq.QuadPart); for (int idx = 0; idx < COUNT; idx++) { if (nickName_CPU[idx] != nickName_GPU[idx]) { puts("CPU和GPU的计算结果不相符!"); getchar(); return 0; } } puts("测试结束"); getchar(); return 0; }
版权声明:本博客所有文章均为原创,欢迎交流,欢迎转载;转载请勿篡改内容,并且注明出处,谢谢!
时间: 2024-10-13 22:28:34