注册 登录
编程论坛 C++教室

c++学习笔记

ehszt 发布于 2017-07-01 14:15, 4695 次点击
也学别人专门开个贴子记录c++中自己碰到的有意思的东西
下面是我写的一个重载程序,我想问问虚函数和重载最大的区别是不是在于重载函数是用同名函数完成相似的功能,而虚函数是用同名函数完成不
同的功能?
#include <iostream>
#include <string.h>
using namespace std;
int max(int x,int y)
{
    return (x>y)?x:y;
}
double max(double a,double b)
{
    return (a>b)?a:b;
}
char *max(char *a,char *b)
{
    if(strcmp(a,b)>0)return a;
    return b;
}
main()
{
    int a,b;
    double m,n;
    char s1[10],s2[10];
    cout<<"please enter the int number:\n";
    cin>>a>>b;
    cout<<max(a,b)<<endl;
    cout<<"please enter the float number:\n";
    cin>>m>>n;
    cout<<max(m,n)<<endl;
    cout<<"please enter the string:\n";
    cin>>s1>>s2;
    cout<<max(s1,s2)<<endl;
}
再加个刚写的try catch异常处理程序,感觉这个try catch有点类似goto。
#include <iostream>
using namespace std;
main()
{
    int a,b;
    while(1)
    {
        try{
        cout<<"请输入两个整数a和b:"<<endl;
        cin>>a>>b;
        if(b==0)throw 0;
        cout<<a<<"/"<<b<<"="<<a/b;
        }
        catch(int)
        {
        cout<<"b不能为“0”请重新输入a和b!"<<endl;
        }
    }
   
}


[此贴子已经被作者于2017-7-1 20:58编辑过]

21 回复
#2
rjsp2017-07-01 15:18
你这几个max是overload
虚函数的是override

异常机制特复杂,包括对象的释放,调用栈的展开 等等,而且还要保证这套异常机制在异常不发生的情况下没影响到运行效率。
#3
ehszt2017-07-01 20:55
这个c++排序程序就这么点,我摘抄下来的。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
main()
{
    vector <int> numlist;
    int element;
    while(cin>>element) //当输入不为整型结束循环
    numlist.push_back(element);
    sort(numlist.begin(),numlist.end()); //这个我猜是algorithm头文件中的排序算法
    for(int i=0;i<numlist.size();i++)
        cout<<numlist[i]<<" ";
}
#4
ehszt2017-07-01 21:02
回复 2楼 rjsp
override要覆盖一个虚函数?

[此贴子已经被作者于2017-12-29 11:18编辑过]

#5
ehszt2017-07-02 14:01
c++文件处理,少了打开方式选项
实测a.txt中的数据,它总要少读取一个
#include <fstream>
#include <iostream>
using namespace std;
main()
{
    int d,sq;
    ifstream fin("c:\\a.txt");
    ofstream fout("c:\\b.txt");
    fin>>d;
    while(!fin.eof())
    {
        sq=d*d;
        cout<<d<<"*"<<d<<"="<<sq<<endl;
        fout<<d<<"*"<<d<<"="<<sq<<endl;
        fin>>d;
    }
    fin.close();
    fout.close();
 }

[此贴子已经被作者于2017-7-2 14:08编辑过]

#6
rjsp2017-07-02 20:15
回复 5楼 ehszt
打开方式是有的,只是可以缺省不写。

    fin>>d;
    while(!fin.eof())
    {
        ……
        fin>>d;
    }
   
改为

    while( fin>>d )
    {
        ……
    }
   

#7
ehszt2017-07-02 22:14
回复 6楼 rjsp
原来这样也行,谢谢了。
#8
ehszt2017-07-04 16:46
插个小故事,我觉得和学习有点关系。
小明想学乒乓球就报了个培训班。可报名交了钱去学习时教练什么都没说就让小明拿个球拍去跟别人打,小明纳闷:我交了那么多钱,怎么一句都不教我就让我
跟别人打?虽然有点纳闷小明还是照做了。小明笨拙的握着球拍把球拍来打去,不是打球没力,就是用力过猛,有时甚至连球都碰不到。过了半个小时,教练就
开始讲了,怎样握拍好打,新手发球都有些什么要领……这么一讲小明就有些懂了。如果教练一开始就讲,小明不一定能明白。所以说学习前先要熟悉那个情景,
不管你“打”的多烂,总要试试,不然老师讲什么你可能听不懂!

[此贴子已经被作者于2017-7-4 20:13编辑过]

#9
ehszt2017-07-05 10:25
c++输入一行字符串
#include <iostream>
#include <string>
using namespace std;
main()
{
    string a;  //这里a的初始化大小为0,不像char数组有一个大小
    getline(cin,a);  //这里字符串a会根据输入长度调整大小,不会存在溢出。
    cout<<a<<endl;
}
#10
ehszt2017-07-09 12:04
c++98新增了模板类vector,它是动态数组的替代器,存储在自由存储区或堆中。c++11新增了模板类array,它是定长数组的替代品,存储在栈中。
用法:
vector<typeName> vt(n_elem);
array<typeName,n_elem> arr;
vector中n_elem可以是变量也可以是常量。
array中n_elem只能是常量。
#11
ehszt2017-12-14 20:25
发个简单的程序,有简单的template用法,简单的派生类
#include <string.h>
#include <vector>
using namespace std;
class stu
{
    private:
        string name;
        double math;
        double English;
        double other;
        
    public:
        stu(string a,double b,double c,double d){
            name=a,math=b,English=c,other=d;
        }
        dis()
        {
            cout<<"the student's name is:"<<name<<endl;
            cout<<"his/her math score is:"<<math<<endl;
            cout<<"his/her English score is:"<<English<<endl;
            cout<<"his/her other score is:"<<other<<endl;
        }
};
class teacher : public stu
{
    private:
        double salary;
    public:
        teacher(string a,double b,double c,double d,double s):stu(a,b,c, d)
            {
            salary=s;
        }
        dis()
        {
            cout<<"teacher's salary is "<<salary<<" One month"<<endl;
        }
        
};
template <typename T>T add(T a,T b)
{
    T c=a+b;
    return c;
}
main()
{
    teacher stud{"jack",75.4,65,86,2500};
    stu td=(stu)stud;
    td.dis();
    stud.dis();
    float a,b;
    int k,j;
    cin>>a>>b;
    cout<<"a+b="<<add(a,b)<<endl;
    cin>>k>>j;
    cout<<"k+j="<<add(k,j)<<endl;
}

[此贴子已经被作者于2017-12-15 19:26编辑过]

#12
ehszt2017-12-28 15:18
拆分单词
#include <string>
#include <iostream>
#include <sstream>
 using namespace std;
 main()
 {
     string a="What a lovely day!";
     istringstream instr(a);
     string word;
     while(instr>>word)
     cout<<word<<endl;
     
 }
这种循环
#include <iostream>
 using namespace std;
 main()
 {
     float a[10]={65.5,66,70,82,54,68,88,90,72,73.5};
     for(float x:a) //想修改数组元素用&x
     cout<<x<<" ";
     
 }

[此贴子已经被作者于2017-12-28 19:44编辑过]

#13
ehszt2017-12-30 17:28
字母全排列
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
    string letters;
    do
    {
        int count=0;
        cout<<"Enter the sequence (quit to quit):";
        cin>>letters;
        cout<<"Permutations of"<<letters<<endl;
        sort(letters.begin(),letters.end());
        cout<<letters<<endl;
        count++;
        while(next_permutation(letters.begin(),letters.end()))
        {
            count++;
            cout<<letters<<endl;
        }
        
        cout<<"上述字母组合一共有"<<count<<"种排法"<<endl;   
    }while(letters!="quit");
    cout<<"Done.\n";
    return 0;
   
}
#14
ehszt2018-01-07 16:37
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
    int a=4,b=3;
    double c;
    c=1.0*a/b;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<c;//这里如果不用setiosflags(ios::fixed)就是保留有效数字位数,如果用就是保留小数位位数。
}
#15
ehszt2018-01-08 16:55
相同类的对象可以用“=”号来赋值,为什么又要弄出一个复制构造函数?
#16
rjsp2018-01-09 08:21
回复 15楼 ehszt
只有一点点相同

复制构造函数( const Type& other ) : 分配资源并初始化
{
}

Type& operator=( const Type& other )
{
    if( this != &other )
    {
        释放自身的资源
        分配资源并赋值
    }
    return *this;
}
#17
ehszt2018-01-09 09:09
好像 建立了对象后就分配了资源。不是很懂,先记下来。
#include <iostream>
using namespace std;
class t{
    private:
        int a;
        int b;
        public:
            t():a(0),b(0){
            };
            t(int m,int n){
                a=m;b=n;
            }
            dis(){
                cout<<a<<" "<<b<<endl;
            }
};
 main()
{
    t A(5,8),B;
    B=A;        //这是用赋值方法复制对象。
    B.dis();
}
用赋值运算符得到两个相同地址的对象,而复制构造函数为新对象分配了内存空间,是生产者。

[此贴子已经被作者于2018-3-10 12:17编辑过]

#18
ehszt2018-02-20 18:39
#include <iostream>
#include <cstdarg>  //这个c和c++中都可以用
using namespace std;
int add(int a, ...)
{
    int s=0,m;
    va_list arg;
    va_start(arg,a);         //初始化va_list  
    for(int i=0;i<a;i++)
    {
        m=va_arg(arg,int);  //读取va_list参数
        s+=m;
    }
    va_end(arg);  //释放va_list
    return s;
}

main()
{
    int a[]={12,31,6,31,4};   //计算不定数组a中元素的和
    cout<<add(sizeof(a)/sizeof(a[0]),12,31,6,31,4);
   
}
#19
ehszt2018-03-08 14:34
快排
#include <iostream>
#include <vector>
using namespace std;
int cmp(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}
main()
{
    int a[10]={1,2,4,5,7,8,9,3,12,11};
    qsort(a,10,sizeof(a[0]),cmp);
    for(int m:a)
    cout<<m<<' ';
}
#20
ehszt2018-03-11 20:11
#include <iostream>
using namespace std;
class t{
    private:
        int a;
        int b;
        public:
            t():a(0),b(0){
            };
            t(int m,int n){
                a=m;b=n;
            }
            dis(){
                cout<<a<<" "<<b<<endl;
            }
};
 main()
{
    t A(5,8),B;
    B=A;        //这是用赋值方法复制对象。
    B.dis();
    A.dis();
    cout<<&A<<" "<<&B<<endl; //输出发现两个对象地址并不相同。
}
#21
ehszt2018-11-08 22:15
写了一个简单的类继承程序
#include <iostream>
#define PI 3.14159
using namespace std;

class point
{
    private: int a;
             int b;
    public:
        point(int m,int n):a(m),b(n){
        };
        show(){
            cout<<"该圆柱的底面圆心坐标是:"<<"\nX="<<
            a<<"  Y="<<b<<endl;
        }
};
class circle:public point
{
    int r;
    public:
        circle(int m,int n,int a):point(m,n){r=a;}
    double getsquare(){return PI*r*r;}
    virtual void display(){cout<<"圆柱底面面积为:"<<PI*r*r<<endl; //如果取消此处的virtual关键字,bk->display();ck->display();打印的都是底面积
    }
};
class yuanzhu:public circle
{
    int H;
    public:
        yuanzhu(int m,int n,int a,int b):circle(m,n,a){
            H=b;
        }
    double    getvolume(){
        return getsquare()*H;
        }
    virtual    void display(){
            cout<<"圆柱的体积为:"<<getvolume()<<endl;
        }
};
main()
{
    yuanzhu ak(2,3,1,10);
    circle m(2,3,1);
    ak.display();
    ak.circle::display();
    ak.show();
    circle *bk=&ak;
    circle *ck=&m;
    bk->display();
    ck->display();
   
}

[此贴子已经被作者于2018-11-11 12:46编辑过]

#22
ehszt2018-11-11 13:05
书上例题:
class shape{
    public:
        virtual double area()=0;
};

class square:public shape{
    protected:
        double H;
    public:
        square(double i){H=i;}
        double area(){return H*H;
        }        
};

class circle:public square{
    public:
        circle(double r):square(r){
        }
        double area(){return H*H*3.14159;
        }
};

class triangle:public square{
    protected:
        double W;
    public: triangle(double h,double w):square(h){W=w;
    }
            double area(){return H*W*0.5;
            }
};

class rectangle:public triangle{
    public:
        rectangle(double h,double w):triangle(h,w){
        }
        double area(){return H*W;
        }
};

double total(shape *s[],int n)
{
    double sum=0.0;
    for(int i=0;i<n;i++)
    {
        sum+=s[i]->area();
    }
    return sum;
}

#include <iostream>
using namespace std;
 main()
{
    shape *s[5];
    s[0]=new square(4);        s[1]=new triangle(3,6);
    s[2]=new rectangle(3,6);    s[3]=new square(6);
    s[4]=new circle(10);
    for(int i=0;i<5;i++)
    {
        cout<<"s["<<i<<"]="<<s[i]->area()<<endl;
    }
    double sum=total(s,5);
    cout<<"The total area is:"<<sum<<endl;
}
1