| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4240 人关注过本帖, 1 人收藏
标题:c++学习笔记
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
发个简单的程序,有简单的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编辑过]

2017-12-14 20:25
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
拆分单词
#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编辑过]

2017-12-28 15:18
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
字母全排列
#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;
   
}
2017-12-30 17:28
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#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)就是保留有效数字位数,如果用就是保留小数位位数。
}
2018-01-07 16:37
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
相同类的对象可以用“=”号来赋值,为什么又要弄出一个复制构造函数?
2018-01-08 16:55
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 15楼 ehszt
只有一点点相同

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

Type& operator=( const Type& other )
{
    if( this != &other )
    {
        释放自身的资源
        分配资源并赋值
    }
    return *this;
}
2018-01-09 08:21
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
好像 建立了对象后就分配了资源。不是很懂,先记下来。
#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编辑过]

2018-01-09 09:09
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#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);
   
}
2018-02-20 18:39
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
快排
#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<<' ';
}
2018-03-08 14:34
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#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; //输出发现两个对象地址并不相同。
}
2018-03-11 20:11
快速回复:c++学习笔记
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016632 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved