C/C++基本语法,入门及提高(2)

来源:互联网 发布:短信阿里云授权服务 编辑:程序博客网 时间:2024/06/16 15:17

C/C++实现简单的算法

> 结构体初始化及使用:指针使用前都必须初始化,结构体中的成员指针也是一样
(1)类型与变量是不同的概念,不要混淆。只能对结构体变量中的成员赋值,而不能对结构体类型赋值。
(2)对结构体变量中的成员(即“域”),可以单独使用,它的作用与地位相当于同类型的普通变量。
(3)结构体的成员也可以是一个结构体变量。
#include<iostream>
using namespace std;
int main(){
 struct Student{      //声明一个结构体类型Student 
  int num;         //声明一个整形变量num 
  char name[20];   //声明一个字符型数组name 
  char sex;        //声明一个字符型变量sex 
  int age;         //声明一个整形变量age 
  float score;     //声明一个单精度型变量 
  char addr[30];   //声明一个字符型数组addr 
 };
   Student student1,student2;// 定义结构体类型变量student1和student2
   cout<<sizeof(Student)<<endl;
   cout<<sizeof(student1)<<endl;
   cout<<sizeof(student2)<<endl;     
   return 0;
}// 68,68,68

初始化:
常用 1)struct Student{};Student student1,student2;// 定义结构体类型变量student1和student2 
常用 2)struct Student{}student1,student2;//声明变量student1和student2
3)struct {}student1,student2;//声明变量student1和student2

#include<iostream>
using namespace std;
 struct Date{        //声明一个结构体类型Date 
  int month;      //日期中的月份 
  int day;        //日期中的天 
  int year;       //日期中的年份 
 }; 
 struct Student{      //声明一个结构体类型Student 
  int num;         //声明一个整形变量num 
  char name[20];   //声明一个字符型数组name 
  char sex;        //声明一个字符型变量sex 
  int age;         //声明一个整形变量age 
  Date birthday;   //Date是结构体类型,birthday是Date的类型的变量 
  float score;     //声明一个单精度型变量 
  char addr[30];   //声明一个字符型数组addr 
 };
int main(){
   Student two={1,"qianshou",'m',19,10,01,1993,100,"JiNan"};
   Student one=two;
   cout<<one.num<<endl;
   cout<<one.name<<endl;
   cout<<one.sex<<endl;
   cout<<one.age<<endl;
   cout<<one.birthday.month<<"/"<<one.birthday.day<<"/"<<one.birthday.year<<endl;
   cout<<one.score<<endl;
   cout<<one.addr<<endl; 
   return 0;
}

#include <stdio.h>
struct student_st{
    char c;
    int score;
    const char *name;
};
static void show_student(struct student_st *stu){
    printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name);
}
int main(void){
    // method 1: 按照成员声明的顺序初始化
    struct student_st s1 = {'A', 91, "Alan"};
    show_student(&s1);
 
    // method 2: 指定初始化,成员顺序可以不定,Linux 内核多采用此方式
    struct student_st s2={
    s2.c = 'B',
    s2.score=92,
    s2.name="yuyu",
    };
    show_student(&s2);
    /*
struct student_st s2 ={
        .c = 'B',
        .score = 92,
        .name = "YunYun",
    };
    show_student(&s2);*/
 
    // method 3: 指定初始化,成员顺序可以不定
    struct student_st s3 = {
        c: 'C',
        score: 93,
        name: "Wood",
    };
    show_student(&s3);  
    return 0;
}
> 指针与结构及内存的分配和回收
#include <iostream>
struct student{   
  char *name;   
  int score;   
  struct student* next;   
}stu,*stu1;    
  
int main(){    
  stu.name = (char*)malloc(sizeof(char)); /*1.结构体成员指针需要初始化*/  
  strcpy(stu.name,"Jimy");   
  stu.score = 99;   
  
  stu1 = (struct student*)malloc(sizeof(struct student));/*2.结构体指针需要初始化*/  
  stu1->name = (char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/  
  stu.next  = stu1;   
  strcpy(stu1->name,"Lucy");   
  stu1->score = 98;   
  stu1->next = NULL;   
  printf("name %s, score %d \n ",stu.name, stu.score);   
  printf("name %s, score %d \n ",stu1->name, stu1->score);   
  free(stu1);   
  return 0;   
}  

> 构造函数与析构函数 C
 在一般讲解C++的书籍中都会提及到当我们不为类提供任何构造与析构函数时编译器会默认提供这样六种成员函数:不带参构造,拷贝构造,“=”的重载函数,析构函数,以及带const和不带const的取地址符重载。
  在C++中,struct可以当做类(class)来使用,同样支持成员函数,构造函数,析构函数。所以可以写对应的构造函数,实现自由初始化。
  析构函数的作用并不是删除对象,而是在撤销对象占用的内存之前完成一些清理工作,使这部分内存可以被程序分配给新对象使用。程序设计者事先设计好析构函数,以完成所需的功能,只要对象的生命期结束,程序就自动执行析构函数来完成这些工作。

C++构造函数与析构函数的解析- http://blog.csdn.net/u011392772/article/details/42772697
#include <iostream>
using namespace std;
class Time{
   public :
   Time( ){
      hour=0;
      minute=0;
      sec=0;
   }
   void set_time( );
   void show_time( );
   private :
   int hour;
   int minute;
   int sec;
};
void Time::set_time( ){
   cin>>hour;
   cin>>minute;
   cin>>sec;
}
void Time::show_time( ){//在类外定义构造成员函数,要加上类名Time和域限定符“::”
   cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( ){
   Time t1;
   t1.set_time( );
   t1.show_time( );
   Time t2;
   t2.show_time( );
   return 0;
}

C++的构造函数和析构函数- http://www.cnblogs.com/luxiaoxun/archive/2012/09/06/2673249.html
/**
new的时候,其实做了三件事,一是:调用::operator new分配所需内存。二是:调用构造函数。三是:返回指向新分配并构造的对象的指针。
delete的时候,做了两件事,一是:调用析构函数,二是:调用::operator delete释放内存。
**/
#include<string>
#include<iostream>
using namespace std;
class Student //声明Student类
{
   public :
   Student(int n,string nam,char s ) //定义构造函数
   {
      num=n;
      name=nam;
      sex=s;
      cout<<"Student class, Constructor called."<<endl; //输出有关信息
   }
   ~Student( ) //定义析构函数
   {
      cout<<"Student class,Destructor called. The num is "<<num<<"."<<endl;
   } //输出有关信息
   void display( ) //定义成员函数
   {
      cout<<"num: "<<num<<endl;
      cout<<"name: "<<name<<endl;
      cout<<"sex: "<<sex<<endl<<endl;
   }
   private :
   int num;
   string name;
   char sex;
};
int main( ){
   Student stud1(10010,"Wang_li",'f'); //建立对象stud1
   stud1.display( ); //输出学生1的数据
   Student stud2(10011,"Zhang_fun",'m'); //定义对象stud2
   stud2.display( ); //输出学生2的数据
   return 0;
}

#include <iostream>
using namespace std;
class MyClass{
    public:
    MyClass(){
        cout << "MyClass Constructors" << endl;
    }
    ~MyClass(){
        cout << "MyClass Destructors" << endl;
    }
};
int main(){
    MyClass* pMyClass =  new MyClass;
    pMyClass->~MyClass();
    delete pMyClass;
    return 0;
}
/**
MyClass Constructors
MyClass Destructors  //这个是显示调用的析构函数
MyClass Destructors  //这个是delete调用的析构函数
**/

C++学习篇——构造函数与析构函数 - http://blog.chinaunix.net/uid-20773165-id-1847729.html
#include <iostream>    
#include <string>    
using namespace std;      
class Teacher{    
    public:    
    Teacher(char *temp)    
    {    
        director = new char[10];    
        strcpy(director,temp);    
    }  
    ~Teacher()    
    {    
        cout<<"释放堆区director内存空间1次 \n";    
        delete[] director;    
        cin.get();  
    }    
    char *show();    
    protected:    
    char *director;    
};    
char *Teacher::show(){    
    return director;    
}    
class Student{    
    public:    
    Student(char *temp,int &pk):teacher(temp),pk(pk),ps(10)  
    {    
        number = 1;    
        score = 100;  
    }    
    void show();    
    
    protected:    
    int number;    
    int score;    
    Teacher teacher;  
    int &pk;  
    const int ps;  
};    
void Student::show(){    
    cout<<teacher.show()<<endl<<number<<endl<<score<<endl<<pk<<endl<<ps<<endl;    
}    
int main(){    
    char *t_name="王大力";  
    int b=99;  
    Student a(t_name,b);  
    a.show();  
    cin.get();  
    return 0;
}

原创粉丝点击