C++
kkqwert
这个作者很懒,什么都没留下…
展开
-
对象数组..
#include <iostream>using namespace std;class exam{public: exam() //一定要有空的构造函数,一开始对象数组里没有输入值,要去找无参的构造函数,找不到就报错 { } exam(int n) { m_x=n; } int getx() { return m_x; }private: int m_x;};.原创 2021-11-04 16:09:12 · 129 阅读 · 0 评论 -
定义一个图书类Book, 用数组记录数据
#include <iostream>using namespace std;class Book{ public: void setbool(string xname ,string xauthor,double xprice)//xname ,xauthor ,xprice 都是形参,用来接收我在main函数里实参的值,来赋给私有的成员变量。 { name=xname; author=xauthor; ...原创 2021-10-20 20:38:47 · 702 阅读 · 0 评论 -
存折类..
请定义“存折”类,及其相关的客户端代码。请注意编写正确的构造函数。存折类的基本情况如下:Type name:AccountTypeDomain: Id,name,balance;Operations: Deposit, 存钱操作; Withdraw,取钱操作; getBalance,获取余额操作; WriteAccount,打印帐户信息;Input:13001 peter 01200 //存的钱500 //取的钱Output:13001 peter 700输入130...原创 2021-10-18 21:39:37 · 497 阅读 · 0 评论 -
构造函数和析构函数
C++使用成员函数给数据成员赋初值既不方便也容易忘记,甚至可能出错,C++提供了一个更好的方法,利用构造函数来完成对象的初始化 构造函数是一种特殊的成员函数,它主要用于为对象分配空间,进行初始化注意:1.构造函数的名字必须与类名相同 2.他可以有任意类型的参数,但不能具有返回值类型 3.他不需要用户调用,而是在建立对象时自动执行的#include <iostream>#include<cma...原创 2021-10-17 20:15:31 · 104 阅读 · 0 评论 -
门牌号(取多位数各个位数)
#include <iostream>using namespace std;int main(){ int res=0; for(int i=1;i<=2020;i++) { int tmp=i; while(tmp) { res+=(tmp%10==2);//遍历四遍,第一次遍历四位数时,找到最后一位的2的个数,第二次遍历3位数,找到最后一位2的个数 tmp/=10;.原创 2021-10-14 17:59:56 · 231 阅读 · 0 评论 -
sort()
是一种类似快速排序的方法,时间复杂度为n*log2(n)在C++中使用sort()函数需要使用#include<algorithm>头文件模板:sort(begin,end,cmp) begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针,cmp参数是排序准则(默认为从小到大排序),从大到小排序可以将cmp参数写为greater<int>()大佬关于sort函数的讲解:C++ sort()排...原创 2021-10-13 15:37:43 · 42 阅读 · 0 评论 -
push—back函数
push_back()函数的用法函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素push_back() 在Vector最后添加一个元素(参数为要插入的值)原创 2021-10-05 11:00:04 · 1013 阅读 · 0 评论 -
给出三个整数 a, b, c, 如果它们可以构成三角形,返回 true.
bool isValidTriangle(int a, int b, int c) { if(a+b>c&&a+c>b&&b+c>a){ return true; }else{ false; } }原创 2021-09-16 21:47:28 · 185 阅读 · 0 评论 -
反转一个三位数
int main(int number) { int bai=number/100; cout<<"百分位的值为:"<<bai; int shi=(number/10)%10; cout<<"十分位的值为:"<<shi; int ge=number%100%10; cout<<"个位的值为:"<<.原创 2021-09-16 15:38:43 · 94 阅读 · 0 评论 -
函数的重载
#include <iostream>//函数重载,同名的函数,不同的参数类型,根据赋值的值的类型去走不同的函数using namespace std;int square(int i){ return i*i;}long square(long l){ return l*l;}double square(double d){ return d*d;}int main(){ int i=12; long l=1234; .原创 2021-09-15 15:12:31 · 31 阅读 · 0 评论 -
按数值由小到大的次序输出这两个数
#include <iostream>using namespace std;float a,b;int main(){ cin>>a; cin>>b; if(a<b) { cout<<a<<b; }else{ cout<<b<<a; }}#include <iostream>using namespace st.原创 2021-09-15 14:10:26 · 241 阅读 · 0 评论 -
关于函数声明
在C++中,如果函数调用的位置在函数定义之前,则要求在函数调用之前必须对所调用的函数作函数原型声明int add(int a,int b);int add();add();//当add函数的返回类型是整型时,可以省略int原创 2021-09-15 14:03:16 · 134 阅读 · 0 评论 -
类的大结构
#include<iostream>using namespace std;//使用命名空间stdclass A{ //声明一个类,类名为A int x,y,z; //声明类A的数据成员 ..... fun() //声明类A的成员函数fun { ..... }};int main(){ A xingcan; //定义类A的一个对象 .原创 2021-09-12 14:07:47 · 49 阅读 · 0 评论