C++语言基础 例程 类的声明和对象的定义

简介: 贺老师的教学链接  本课讲解类的声明和对象的定义-形式1#include <iostream>#include <cstring>using namespace std;class Student{private: int num; char name[20]; char sex;public: void set_da

贺老师的教学链接  本课讲解


类的声明和对象的定义-形式1

#include  <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
    int num;
    char name[20];
    char sex;
public:
    void set_data(int n, char *p,char s)
    {
        num=n;
        strcpy(name,p);
        sex=s;
    }
    void display( )
    {
        cout<<"num: "<<num<<endl;
        cout<<"name: " <<name<<endl;
        cout<<"sex: " <<sex<<endl<<endl;
    }
};
int main()
{
    Student stud1,stud2;
    stud1.set_data(1,"He",'f');
    stud2.set_data(2,"She",'m');
    stud1.display();
    stud2.display();
    return 0;
}


类声明的第2种形式——更实用和常见
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
public:
    void set_data(int n, char *p,char s);
    void display( );
private:
    int num;
    char name[20];
    char sex;
};
void Student::set_data(int n, char *p,char s)
{
    num=n;
    strcpy(name,p);
    sex=s;
}
void Student::display( )
{
    cout<<"num: "<<num<<endl;
    cout<<"name: " <<name<<endl;
    cout<<"sex: " <<sex<<endl<<endl;
}
int main()
{
    Student stud1,stud2;
    stud1.set_data(1,"He",'f');
    //stud1.sex='m';
    //strcpy(stud1.name,"You");
    stud2.set_data(2,"She",'m');
    stud1.display();
    stud2.display();
    return 0;
}


并非所有成员函数必须公有
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );
    void show_time( );
private:
    bool is_time(int, int, int);
    int hour;
    int minute;
    int sec;
};

void Time::set_time( )
{
    char c1,c2;
    cout<<"请输入时间(格式hh:mm:ss)";
    while(1)
    {
        cin>>hour>>c1>>minute>>c2>>sec;
        if(c1!=':'||c2!=':')
            cout<<"格式不正确,请重新输入"<<endl;
        else if (!is_time(hour,minute,sec))
            cout<<"时间非法,请重新输入"<<endl;
        else
            break;
    }
}

void Time::show_time( )
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}

bool Time::is_time(int h,int m, int s)
{
    if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
        return false;
    return true;
}
int main( )
{
    Time t1;
    t1.set_time( );
    t1.show_time( );
    return 0;
}


目录
相关文章
|
19小时前
|
C++
C++ 类的初始化列表与构造函数初始化的技术性探讨
C++ 类的初始化列表与构造函数初始化的技术性探讨
5 0
|
1天前
|
编译器 C++ 容器
C++语言的基本语法
学习C++编程首先需熟悉环境和语法,包括理解对象(具有状态和行为)、类(对象的模板)、方法(描述行为)及即时变量(定义对象状态)。C++程序始于`main()`函数,如示例代码所示,通过`#include`引入头文件,使用`std`命名空间。程序结构包括定义、编译和执行步骤,其中分号作为语句结束符,大括号表示语句块。C++标识符由字母、下划线和数字组成,关键词有特定含义,不能作为变量名。了解空格和注释的使用也很重要。最后,`main`函数通常返回`int`类型,`using namespace std;`可简化命名空间引用。
5 0
|
2天前
|
安全 程序员 C语言
从C语言到C++_37(特殊类设计和C++类型转换)单例模式(下)
从C语言到C++_37(特殊类设计和C++类型转换)单例模式
14 5
|
2天前
|
设计模式 编译器 Linux
从C语言到C++_37(特殊类设计和C++类型转换)单例模式(中)
从C语言到C++_37(特殊类设计和C++类型转换)单例模式
12 0
|
4天前
|
存储 编译器 程序员
c++存储类
c++存储类
23 3
|
4天前
|
C++
c++类&对象
c++类&对象
19 3
|
8天前
|
C++
C++派生类
C++派生类
23 0
|
8天前
|
存储 程序员 数据安全/隐私保护
C++类
C++类
20 0
|
9天前
|
设计模式 安全 Java
【C++】特殊类设计
【C++】特殊类设计
|
2天前
|
安全 编译器 C语言
从C语言到C++_37(特殊类设计和C++类型转换)单例模式(上)
从C语言到C++_37(特殊类设计和C++类型转换)单例模式
11 0