
c/c++
赖卓成
这个作者很懒,什么都没留下…
展开
-
c++重载运算符实现字符串
/*若使用浅拷贝,两个对象的data指针指向同一块内存空间,当对象析构的时候,会释放两次同一个地址的内存,出现程序coredump解决方法1:重写拷贝构造函数,当用一个对象初始化另外一个对象时,会调用拷贝构造函数,在拷贝构造函数内执行深拷贝############################################################################################################################方法1只是解决了.原创 2020-11-29 15:31:34 · 976 阅读 · 0 评论 -
C++运算符(+,前置++,后置++,<<)重载,操作复数
#include<iostream>using namespace std;class Complex{//类外不能访问类的私有成员 所以使用友元函数friend Complex& operator+(Complex &c1,Complex &c2); friend Complex& operator++(Complex& c1);friend Complex& operator++(Complex& c1,int);fr.原创 2020-11-29 02:09:29 · 1025 阅读 · 0 评论 -
c++深拷贝浅拷贝问题
/*若使用浅拷贝,两个对象的data指针指向同一块内存空间,当对象析构的时候,会释放两次同一个地址的内存,出现程序coredump解决方法1:重写拷贝构造函数,当用一个对象初始化另外一个对象时,会调用拷贝构造函数,在拷贝构造函数内执行深拷贝############################################################################################################################方法1只是解决了.原创 2020-11-29 01:42:51 · 311 阅读 · 0 评论 -
单目运算符重载为友元函数
#include<iostream>using namespace std;class point{private: int x,y;public: point(int xx=0,int yy=0) {x=xx;y=yy;} void display(){cout<<"x="<<x<<",y="<&a原创 2018-07-23 15:39:22 · 1901 阅读 · 1 评论 -
删除数组内所有相同成员至只剩下一个并输出数组
//先排序 再比较 将相同的元素放到最后 输出的时候不输出相同元素即可 #include<iostream>using namespace std;int sum=0;void display(int *a,int n){ for(int i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;}void px(int ...原创 2018-06-07 23:56:36 · 846 阅读 · 0 评论 -
设有一描述时钟的clock类,它的私有成员hour,minute,second分别代表小时 分钟和秒,程序中要求包括构造函数 析构函数 拷贝构造函数 以及设置时间的函数 显示时间 的函数
#include<iostream>using namespace std;class clock{private: int hour,minute,second;public: clock(int a,int b,int c) {hour=a;minute=b;second=c;} ~clock() {cout<<"释放内存"<<endl;} void dis...原创 2018-06-07 16:57:06 · 7407 阅读 · 0 评论 -
定义一个shape类,在此基类基础上派生Rectangele 和circle类,二者都有getarea()函数计算对象的面积,使用Rectangele 类创建一个派生类square类。请设计各个类并进
#include<iostream>using namespace std;class shape{public: virtual double getarea()=0;};class Rectangele:public shape{private: double longth; double width;public:Rectangele(double a,double b){ lo...原创 2018-05-31 14:38:00 · 15424 阅读 · 4 评论 -
定义一个Dog类,它用静态数据成员记录Dog的个体数目,静态成员函数来存取Dogs。设计并测试这个类
#include<iostream>using namespace std;class Dog{private: static int Dogs;public: static getDogs(); Dog() {Dogs++;}}; int Dog::getDogs() {return Dogs;} int Dog::Dogs=0;void main(){ Dog A,B,C; cou...原创 2018-05-29 14:33:13 · 5007 阅读 · 2 评论 -
使用静态数据成员
#include<iostream>using namespace std;class test{int k;public:static int n;//静态数据成员test(int kk){k=kk;n++;}//类内直接引用静态数据成员void disp(){cout<<"n="<<n<<"k="<<k<&l...原创 2018-05-13 15:56:55 · 851 阅读 · 0 评论 -
游泳池 圆形外栅栏35元每米 过道3米宽 秀过道20元每平方米 输入半径求造价
#include<iostream>using namespace std;class yyc{private: float r;public: yyc() { cout<<"请输入游泳池半径:"; cin>>r; cout<<"米"<<endl; } float zl() { return (35*(2*(r+3)*3.原创 2018-05-05 08:36:00 · 1176 阅读 · 0 评论 -
友元成员(函数)
#include<iostream>#include<string.h>using namespace std;class boy;class girl{private: char *name; int age;public: girl(char *n,int a) { name=new char[strlen(n)+1]; strcpy(name,n); age=a;...原创 2018-05-17 22:17:11 · 208 阅读 · 0 评论 -
构造函数 拷贝构造函数 析构函数
#include<iostream>using namespace std;class student{ private: int id; int age;public: student() { id=1; age=20; } ~student() { cout<<"释放"<<id<<"的内存"<<endl; }原创 2018-05-04 17:36:15 · 109 阅读 · 0 评论 -
矩阵
#include<iostream>#include<iomanip>using namespace std;void main(){ int a[100][100]={0}; int i,j,k; cin>>k; for(i=0;i<k;i++) for(j=0;j<k;j++) { if(i<j&&(i+j)<...原创 2018-04-26 17:46:13 · 165 阅读 · 0 评论 -
用对象指针引用对象数组
#include<iostream.h>//using namespace std;class A{int x;public:void set_x(int a){x=a;}void show_x(){cout<<x<<endl;}};main(){A *ptr,ptr1[2];ptr1[0].set_x(12);ptr1[1].se...原创 2018-05-10 21:06:53 · 1237 阅读 · 0 评论 -
继承中的特殊关系隐藏
#include<iostream>using namespace std;class person{protected: int age;public: void print() {cout<<age<<endl;}/* person() {age=18;} */};class worker:public person{private: int age;pub...原创 2018-06-01 11:35:02 · 186 阅读 · 0 评论 -
this 指向当前对象,通过它可以访问当前对象的所有成员。
#include<iostream>using namespace std;class eg{private: int a;public: eg(int b) {a=b;} void add (int m) { eg p(5); p.a=p.a+m; *this=p;//this指...原创 2018-07-23 14:47:51 · 1821 阅读 · 0 评论 -
c++实现内存修改进程内存 修改马里奥
1.HWND:表示句柄(handle), Wnd 是变量对象描述,表示窗口,所以hWnd 表示窗口句柄2.FindWindow函数返回与指定字符串相匹配的窗口类名或窗口名的最顶层窗口的窗口句柄。这个函数不会查找子窗口。函数原型:HWND FindWindow( LPCTSTR lpClassName,LPCTSTR lpWindowName)//类名,标题名3.DWORD双字DWORD ...原创 2019-03-06 22:41:09 · 8114 阅读 · 0 评论 -
文件的读取
// ConsoleApplication3.cpp: 定义控制台应用程序的入口点。//#include "stdafx.h"#include <fstream>#include <iostream>using namespace std;int main(){ char data[100]; // 以写模式打开文件 ofstr...原创 2018-09-16 20:55:35 · 114 阅读 · 0 评论 -
文件的打开
#include<iostream.h>#include<fstream.h>int main(){ ifstream in; in.open("E:\heisha",ios::nocreate); if(in.fail()) cout<<"文件不存在!"<<endl; in.close(); ...原创 2018-09-16 20:06:48 · 115 阅读 · 0 评论 -
选择排序法
//选择排序法#include<iostream>#define N 10using namespace std;int main(){ int a[N]; void input(int *p); void sort(int *p); void output(int *p); input(a); sort(a); outp...原创 2018-09-09 14:23:52 · 169 阅读 · 0 评论 -
虚基类的使用
#include<iostream>using namespace std;class A{protected:int x;public:A(){x=1;}};class B:virtual public A{public: B() {cout<<"B:x="<<x<<endl;}};class C:vi...原创 2018-08-19 19:17:52 · 838 阅读 · 0 评论 -
运算符重载实现复数的相加减
#include<iostream>using namespace std;class complex{private: double real; double imag;public: complex(double r=0,double i=0):real(r),imag(i){} complex operator + (complex &a...原创 2018-07-23 00:43:37 · 3032 阅读 · 0 评论 -
类的组合:computer
#include<iostream>using namespace std;enum RAM_Type {DDR2=2, DDR3, DDR4};enum CPU_Rank {P1,P2,P3,P4,P5,P6};enum ROM_Type{SATA,USB};enum ROM_Installation{external,builtin};class RAM{priva...原创 2018-07-20 15:01:45 · 891 阅读 · 0 评论 -
枚举类
//不同枚举类中枚举常量值有相同的名字不会发生冲突,普通枚举类型会冲突#include<iostream>using namespace std;enum class Side(Right,Left);enum class Thing(Wrong,Right);int main(){ Side s=Side::Right; Thing w=Thing::R...原创 2018-07-20 01:30:00 · 97 阅读 · 0 评论 -
类的组合 计算线段长度
#include<iostream>#include<math.h>using namespace std;class Point{private: int x,y;public: Point(int xx,int yy) {x=xx;y=yy;} int getx() {return x;} int gety()...原创 2018-07-19 18:12:50 · 1884 阅读 · 0 评论 -
单目运算符重载为成员函数
#include<iostream>using namespace std;class point{private: int x,y;public: point(int xx=0,int yy=0) {x=xx;y=yy;} point operator ++(int)//后置++加上一个int 前置不用加 { x+...原创 2018-07-23 15:08:36 · 794 阅读 · 0 评论 -
用对象指针引用对象数组
#include<iostream>using namespace std;class A{int x;public:void set_x(int a){x=a;}void show_x(){cout<<x<<endl;}};void main(){A *ptr,ptr1;ptr1.set_x(3);ptr1.show_x();p...原创 2018-05-10 20:48:52 · 434 阅读 · 0 评论 -
给类中定义了不带参数的构造函数的对象数组赋值
#include<iostream>using namespace std;class exam{int x;public:exam()//不带参数的构造函数{x=3;}exam(int n)//带参数的构造函数{x=n;}int getx(){return x;}};void main(){ exam op(2);//调用带参数的构造函数cout<...原创 2018-05-10 20:39:56 · 1881 阅读 · 0 评论 -
嵌套函数计算!a+!b+!c
#include<iostream>using namespace std;int fun1(int a){ int i,s=0; for(i=1;i<=a;i++) s=s+i; return s;}int fun2(int a,int b,int c){ return fun1(a)+fun1(b)+fun1(c);}void main(){...原创 2018-04-14 21:49:42 · 790 阅读 · 0 评论 -
函数的重载
#include<iostream>using namespace std;int fun(int a,int b);double fun(double a,double b);int fun(int a,int b){ if(a*b>0) return abs(a-b); if(a*b<0) return abs(a)+abs(b);}double ...原创 2018-04-14 21:32:34 · 228 阅读 · 0 评论 -
统计英文句子中有多少个英文单词 单词之间用空格分开
#include<iostream>#include<string.h>using namespace std;void main(){ int i,j=0; char a[100]; cout<<"请输入一个英文句子"<<endl; gets(a); puts(a); for(i=0;i<strlen(a);i++) { if(a[i]==...原创 2018-04-14 19:03:46 · 10586 阅读 · 0 评论 -
函数模板 取绝对值
#include<iostream>using namespace std;template<typename T>T abs(T a){ if(a<0) return -a; else return a;}void main(){ int a,i; double b,j; char c,k; cin>>a>>b>>c; i=...原创 2018-04-14 14:57:47 · 2146 阅读 · 0 评论 -
从键盘输入一个字符串 使用指针判断字符串中大写字母 小写字母 数字 的个数并输出
#include<iostream>#include<string.h>using namespace std;void fun(char *p){ int i; int a,b,c; a=b=c=0; for(i=0;i<=strlen(p);i++) { if(*(p+i)>='A'&&*(p+i)<='Z') a++; if(*(...原创 2018-04-14 14:35:46 · 8912 阅读 · 0 评论 -
数组名就是数组首地址
#include<stdio.h>#include<string.h>void main(){ char a[20]={"abcdefg"}; char b[20]={"higklmn"}; strcpy(a+7,b); char *x; char *y; x=a+1; y=&a[1]; printf("%s\n",a);// printf("%s\n",b原创 2018-03-22 23:16:40 · 3934 阅读 · 0 评论 -
字母金字塔
#include<iostream>using namespace std;void main(){ int i,j,k; char c='A'; for(i=1;i<=7;i++) { for(j=7-i;j>=0;j--) cout<<" "; for(k=1;k<=2*i-1;k++) cout<<c; cout<<..原创 2018-03-14 08:03:09 · 991 阅读 · 0 评论 -
插入一个数之后再排序
#include<iostream>using namespace std;void main(){ int n; cout<<"请输入需要排序的数的个数"<<endl; cin>>n; int a[9999],i,t,j; cout<<"请给数组赋值"<<endl; for(i=0;i&l原创 2018-03-13 22:25:29 · 708 阅读 · 0 评论 -
冒泡排序
#include<iostream>using namespace std;void main(){ int n; cout<<"请输入需要排序的数的个数"<<endl; cin>>n; int a[9999],i,t,j; cout<<"请给数组赋值"<<endl; for(i=0;i&l原创 2018-03-13 21:20:29 · 131 阅读 · 0 评论 -
指针加函数 交换变量值
#include<iostream>using namespace std;void swap1(int *x,int *y){ int z; z=*x; *x=*y; *y=z;}main(){ int a,b; cin>>a>>b; swap1(&a,&b); cout<<a<<b;}#include<iostr...原创 2018-03-16 19:18:55 · 196 阅读 · 0 评论 -
三个数排序 交换值
#include<iostream>using namespace std;void swap(int &a,int &b){ int t; t=a; a=b; b=t;}void main(){ int a,b,c; cout<<"please input 3 number:"; cin>>a>>b>>c; if(a原创 2018-03-16 20:45:45 · 1495 阅读 · 0 评论 -
写出一个函数,要求将输入的十六进制数转换成十进制数。要求函数调用时,使用指针作函数形参。
#include<iostream>#include<string.h>using namespace std;void main(){ int temp(char *a); char *a=new char[20]; int b; cout<<"请输入一个十六进制数"<<endl; cin>>a; b=temp(a); if(b!=-1...原创 2018-03-27 23:19:18 · 5306 阅读 · 1 评论