码迷,mamicode.com
首页 > 编程语言 > 详细

C++的笔记学习第一篇,认识C++

时间:2014-07-31 20:27:47      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   io   数据   

在一个类中包含两种成员: 数据和函数,分别称为C++数据成员和成员函数。

关于类:

    类是C++新增加的重要数据类型,有了类,就就可以实现面向对象程序设计方法中的封装、信息隐蔽、继承、派生、多态等功能。

关于函数的重载

在编程时,有时我们要实现的是同一类的功能,只是有些细节不同。例如希望从3个数中找出其中的最大者,而每次求最大数时数据的类型不同,可能是3个整数、3个双精度数或3个长整数。

程序设计者往往会分别设计出3个不同名的函数,其函数原型为:
int max1(int a int b, int c); // 3个整数中的最大者
double max2(double a double b double c); // 3个双精度数中最大者
long max3(long a long b long c);// 3个长整数中的最大者

C++允许用同一函数名定义多个函数,这些函数的
参数个数和参数类型不同。这就是函数的重载(function overloading)overloading)。即对一个函数名重新赋予
它新的含义,使一个函数名可以多用

下面就是一个max()函数重载的例子

bubuko.com,布布扣
#include<iostream>
using namespace std;
int main()
{

        int max(int a,int b,int c);
        double max(double a,double b,double c);
        long  max(long a,long b,long c);

        int i1,i2,i3,i;
        cin>>i1>>i2>>i3;
        i = max(i1,i2,i3);
        cout<<"i_max = "<<i<<endl;

        double d1,d2,d3,d;
        cin>>d1>>d2>>d3;
        d = max(d1,d2,d3);
        cout<<"i_max = "<<d<<endl;

        long g1,g2,g3,g;
        cin>>g1>>g2>>g3;
        g = max(g1,g2,g3);
        cout<<"g_max = "<<g<<endl;

        return 0;
}
View Code

 

上例3个max函数的函数体是相同的,其实重载函数并不要求函数体相同。重载函数除了允许参数类型不同以外,还允许参数的个数

bubuko.com,布布扣
例4.6 编写一个程序,用来求两个整数或3个整数中
的最大数。如果输入两个整数,程序就输出这两个
整数中的最大数,如果输入3个整数,程序就输出
这3个整数中的最大数。
#include <iostream>
using namespace std;
int main( )
{
    int max(int a,int b,int c);
    max(int a,int b,int
    //函数声明
    int max(int a,int b);
    max(int a,int
    int a=8,b=-12,c=27;

    cout<< max(a,b,c)=′′<<max(a,b,c)<<endl //输出3个整数中的最大者
    cout<<′′max(a,b,c)=′′<<max(a,b,c)<<endl; //

    cout<< max(a,b)=′′<<max(a,b)<<endl
    cout<<′′max(a,b)=′′<<max(a,b)<<endl;
}


int max(int a,int b,int c)
{
    if(b>a) a=b;
    if(c>a) a=c;
    return a;
}

int max(int a,int b)
{
    if(a>b) 
            return a;
    else
            return b;
}
View Code

 

函数模板的定义:

template < typename T> 或 template <class T>
通用函数定义

bubuko.com,布布扣
#include<iostream>
using namespace std;
template <typename T>
T max(T a,T b,T c)
{
        if(b>a) a= b;
        if(c>a) a = c;
        return a;
}

int main()
{
        int i1 = 185,i2 = -76,i3 = 567,i;
        double d1 = 56.67,d2 = 90.21,d3 = -321.35,d;
        long g1 = 6789,g2 = -912345,g3= 674738,g;
        i = max(i1,i2,i3);
        d = max(d1,d2,d3);
        g = max(g1,g2,g3);
        cout << "i_max = "<<i<<endl;
        cout << "f_max = "<<d<<endl;
        cout << "g_max = "<<g<<endl;
        return 0;
}
View Code

 

sting函数的用法,c++ 里面独有的string变量,使用这个string相当强悍

bubuko.com,布布扣
#include<iostream>
#include <string>
using namespace std;
string name[50],num[50];
int n;
int main()
{
    void input_data();
    void search(string find_name);
    string find_name;
    cout<<"please input number of this class:";
    cin >> n;
    input_data();
    cout<<"please input name you want to find:";
    cin >> find_name;
    search(find_name);
    system("pause");    
    return 0;
}

void input_data()
{
    int i;
    for(i = 0;i<n;i++)
    {
        cout<<"input name an NO.of student"<<i+1<<endl;
        cin>>name[i]>>num[i];
    }
}

void search(string find_name)
{
    int i;
    bool flag = false;
    for(i = 0;i<n ;i++)
    {
        if(name[i] ==find_name)
        {
            cout << name[i] << "has been found,his num is "<<num[i]<<endl;
            flag = true;
            break;
        }
        if(flag == false)
            cout<<"can‘t find this name";
    }
}
View Code

 



 

C++的笔记学习第一篇,认识C++,布布扣,bubuko.com

C++的笔记学习第一篇,认识C++

标签:style   blog   http   color   使用   os   io   数据   

原文地址:http://www.cnblogs.com/fengdashen/p/3880939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!