传智C++课程笔记2

来源:互联网 发布:剑灵不知火舞捏脸数据 编辑:程序博客网 时间:2024/06/03 20:52
#include "struct.hpp"#include <cstring>#include <cstdlib>#include <iostream>///////////////////////////////////////////// reference: https://msdn.microsoft.com/zh-cn/library/64973255.aspxstruct PERSON {   // Declare PERSON struct type    int age;   // Declare member types    long ss;    float weight;    char name[25];} family_member;   // Define object of type PERSONstruct CELL {   // Declare CELL bit field    unsigned short character : 8;  // 00000000 ????????    unsigned short foreground : 3;  // 00000??? 00000000    unsigned short intensity : 1;  // 0000?000 00000000    unsigned short background : 3;  // 0???0000 00000000    unsigned short blink : 1;  // ?0000000 00000000} screen[25][80];       // Array of bit fields int test_struct1(){    struct PERSON sister;   // C style structure declaration    PERSON brother;   // C++ style structure declaration   在C++中struct类型的数据可以像class定义的类一样的使用,并且在struct定义的类型中也可以定义成员函数,而在c语言中不可以这样的定义    sister.age = 13;   // assign values to members    brother.age = 7;    std::cout << "sister.age = " << sister.age << '\n';    std::cout << "brother.age = " << brother.age << '\n';    CELL my_cell;    my_cell.character = 1;    std::cout << "my_cell.character = " << my_cell.character<<'\n';    return 0;}//////////////////////////////////////////////////////////// reference: http://www.tutorialspoint.com/cplusplus/cpp_data_structures.htmstruct Books {    char  title[50];    char  author[50];    char  subject[100];    int   book_id;};void printBook(struct Books book){    std::cout << "Book title : " << book.title << std::endl;    std::cout << "Book author : " << book.author << std::endl;    std::cout << "Book subject : " << book.subject << std::endl;    std::cout << "Book id : " << book.book_id << std::endl;}int test_struct2(){    struct Books Book1;        // Declare Book1 of type Book    struct Books Book2;        // Declare Book2 of type Book    // book 1 specification    strcpy(Book1.title, "Learn C++ Programming");    strcpy(Book1.author, "Chand Miyan");    strcpy(Book1.subject, "C++ Programming");    Book1.book_id = 6495407;    // book 2 specification    strcpy(Book2.title, "Telecom Billing");    strcpy(Book2.author, "Yakit Singha");    strcpy(Book2.subject, "Telecom");    Book2.book_id = 6495700;    // Print Book1 info    printBook(Book1);    // Print Book2 info    printBook(Book2);    return 0;}///////////////////////////////////////////////////////////// reference: http://www.dummies.com/how-to/content/how-to-build-a-structure-template-in-c.htmltemplate<typename T>struct Volume {    T height;    T width;    T length;    Volume()    {        height = 0;        width = 0;        length = 0;    }    T getvolume()    {        return height * width * length;    }    T getvolume(T H, T W, T L)    {        height = H;        width = W;        length = L;        return height * width * length;    }};int test_struct3(){    Volume<int> first;    std::cout << "First volume: " << first.getvolume() << std::endl;    first.height = 2;    first.width = 3;    first.length = 4;    std::cout << "First volume: " << first.getvolume() << std::endl;    Volume<double> second;    std::cout << "Second volume: " << second.getvolume(2.1, 3.2, 4.3) << std::endl;    std::cout << "Height: " << second.height << std::endl;    std::cout << "Width: " << second.width << std::endl;    std::cout << "Length: " << second.length << std::endl;    return 0;}///////////////////////////////////////////////////////// reference: http://www.java2s.com/Code/Cpp/Class/Constructoranddestructorinsideastruct.htmstruct StringClass{    StringClass(char *ptr);    ~StringClass();    void show();private:    char *p;    int len;};StringClass::StringClass(char *ptr){    len = strlen(ptr);    p = (char *)malloc(len + 1);    if (!p) {        std::cout << "Allocation error\n";        exit(1);    }    strcpy(p, ptr);}StringClass::~StringClass(){    std::cout << "Freeing p\n";    free(p);}void StringClass::show(){    std::cout << p << " - length: " << len;    std::cout << std::endl;}int test_struct4(){    StringClass stringObject1("www.java2s_1.com."), stringObject2("www.java2s_2.com.");    stringObject1.show();    stringObject2.show();    return 0;}

**struct PERSON sister; // C style structure declaration
PERSON brother; // C++ style structure declaration 在C++中struct类型的数据可以像class定义的类一样的使用,并且在struct定义的类型中也可以定义成员函数,而在c语言中不可以这样的定义
**
ifferences between C struct & C++ struct:

(1)、 C structure can't contain functions means only data members are allowed, but structure in C++ can have both functions &  data members.(2)、 struct keyword is necessary in C to create structure type variable, but it is  redundant & not necessary in C++.(3)、 Size of empty structure is undefined behavior in C, but it is always 1 in C++.(4)、 Structure in C can't have static members, but C++ structure can have static members.(5)、 Structure members can't be directly initialized inside the struct in C, but it is allowed in C++ since C++11.(6)、 We can have both pointers and references to struct in C++, but only pointers to structs are allowed. (References aren't feature of C language).(7)、 C++ also have .* and -> operators to access individual members of struct, but C doesn't have such kind of operators.(8)、 struct declaration establishes a  scope in C++ and not in C, which makes member enumerators and nested structs possible in C++(you can *write* them inside a struct in C, but they escape and become local to whatever function the parent struct is in).(9)、 In C, we need to use struct tag whenever we declare a struct variable. In C++, the struct tag is not necessary.(10)、C++ structures are very similar to a class, with the only difference being that in a class, all members are private by default. But in a C++ structure, all members are public by default. In C, there is no concept of public or private.(11)、C++ structures can have member functions, whereas C structures cannot.(12)、You can have constructors, destructors, copy constructors and so on in C++ structures. You cannot in C structures.(13)、C++ structures can have static members, whereas C structures cannot.
  1. 求解一个圆还有圆的周长
    编译环境 VS2008
#include <iostream>   //包含C++的头文件using namespace std;    //使用一个标准的命名空间void  main01(){    //printf("hello world!\n");    cout << "hello world!"<<endl;    system("pause");    //return 0;}//编写一个求圆的面积的函数  //使用面向过程的方法求解一个圆的面积以及周长int main(){    double r = 0;   //    double s = 0,grith = 0;    const double pi = 3.141159;    cout<<"请输入圆的半径:";    cin>>r;    grith = 2*pi*r;    s = pi*r*r;    cout<<"圆的半径为:"<<r<<endl;    cout<<"圆的面积为:"<<s<<endl;    cout<<"圆的周长为:"<<grith<<endl;    system("pause");    return 0;}

下面是使用面向对象的方法编写C++程序,实现输入圆的半径输出圆的周长和面积的C++程序

#include <iostream>   //包含C++的头文件using namespace std;    //使用一个标准的命名空间void  main01(){    //printf("hello world!\n");    cout << "hello world!"<<endl;    system("pause");    //return 0;}//编写一个求圆的面积的函数  //使用面向过程的方法求解一个圆的面积以及周长int main02(){    double r = 0;   //    double s = 0,grith = 0;    const double pi = 3.141159;    cout<<"请输入圆的半径:";    cin>>r;    grith = 2*pi*r;    s = pi*r*r;    cout<<"圆的半径为:"<<r<<endl;    cout<<"圆的面积为:"<<s<<endl;    cout<<"圆的周长为:"<<grith<<endl;    system("pause");    return 0;}class Circle{public:  //类的访问控制    double radius;    void Set_Radius(double r){radius  = r;};    double Get_Radius(){return radius;};    double Get_Grith(){return 2*3.14f*radius;};  //通过成员函数获取成员变量    double Get_Area(){return 3.14f*radius*radius;};};//使用面向对象的方法编写一个程序,获取圆的半径,最后输出圆的周长和圆的面积//在C++中变量有一个好听的名字叫做属性int main(){     Circle A,B;    //用类定义两个对象     double r = 0,R =0;     cout  << "请输入R1的值:";     cin >> r;     cout  << "请输入R2的值:";      cin >> R;     A.Set_Radius(r);   //类的调用     cout << "A.Radius = " << A.Get_Radius() << endl;      cout << "A.Grith = " << A.Get_Grith() << endl;      cout << "A.Area = "<< A.Get_Area() << endl;      B.Set_Radius(R);   //类的调用      cout << "B.Radius = " << B.Get_Radius() << endl;      cout << "B.Grith = " << B.Get_Grith() << endl;      cout << "B.Area = "<< B.Get_Area() << endl;    system("pause");    //实现让程序调试运行的时候能够停留在这个页面方便程的调试    return 0;}
#include<iostream>using namespace std;//c++的命名空间class circle{public:    double r;    double pi = 3.1415926;    double area = pi*r*r;};// 2010编译不通过 但是在2013编译器能编译通过int main(){    circle c1;   //c1在对象初始化的时候进行了相应的对象的初始化,也就是进行了////double r;//double pi = 3.1415926;//double area = pi*r*r;//    cout << "please input your r" << endl;    cin >> c1.r;   //这个时候虽然对r进行了赋值但是这时已经不在进行pi和area的运算;    cout << c1.area << endl;    //乱码,在调用的时候其实的得到的是r,没有初始化时的内存空间的值area,所以area是一个很大的值    system("pause");    return 0;}

这里写图片描述

#include <iostream>#include <string.h>#include <stdlib.h>using namespace std;//文件iosream中没有引入标准的  std;需要程序员手工的写int main31(){    system("pause");    return 0;}//怎样定义一个命名空间namespace namespaceA{  int a = 10;}namespace namespaceB{    int a = 20;    namespace namespaceC    {        struct Teacher        {            char name[32];            int age ;        };    }} //使用命名空间int main(){     char buff[25];    int i = 3;    sprintf(buff,"%d",i);    using  namespaceA::a ;    cout <<a << endl;    system("pause");    return 0;}

和C语言相比C++的实用性增加主要是:

  • C语言的变量必须在作用域开始的地方定义C++中更加强调语言的使用性,所有的变量都能够在使用的时候在定义;

register关键字增强
register关键字请求编译器让变量a直接放在寄存器里面,速度快,因此在C语言中register修饰的变量不能够取地址,但是在C++中能够取得register变量的值。

在C++中register主要有一下的变化:

  1. register关键字请求编译器将局部变量存储于寄存器中
    C++编译器有自己的优化方式,不使用register也能够做优化,C++中能够取得register变量的地址。
  2. C++编译器发现程序中需要取register变量地址的时候,register变量的声明变得无效;
  3. 早起的C语言编译器不会对代码进行优化,因此register变量是一个很好的补充;

C++中变量检测增强,在C语言中 重复的定义多个同名的全局变量是合法的,但是在C++中不允许同时定义多个同名的全局变量,在C语言中多个同名的全局变量会被链接到全局数据区域的的同一个地址空间上;

struct类型增强
C语言的struct定义了一组变量的集合,C编译器并不认为这是一种新的类型,C++中struct是一个新类型的定义声明

C++中所有的变量和函数都必须有类型的声明C语言中的默认类型在C++中是不合法的
C++中新增Bool类型的关键字并且C++中Bool类型的数据只有两种取值,true和false,理论上bool只占一个字节。如果多个bool类型放在一起可能各占一个bit,这取决于编译器得到实现,

三目运算符在C和C++编译器的实现

  • 三目运算符在C语言中,返回变量的值,不能作为变量使用,
  • 三目运算符在C++中返回的是变量本身,能够作为左值并能够放在程序的任何地方
  • 但是当三目运算符中返回值有一个可能是常量值的时候,则不能作为左值使用(a < b ? 1 : b) = 30;因为有可能返回的是常量1,因此不能够做左值;

  • -
#include <iostream>using namespace std;struct Teacher{    int a_arg[10];    char s_tring[100];};int main(){    Teacher   a;      //在C++中对struct关键字进行增强,认为struct定义的为一个类型    printf("hello world!\n");    system("pause");          return 0;}