1 Star 0 Fork 0

Sjianzhao / C++学习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
类和对象.cpp 4.38 KB
一键复制 编辑 原始数据 按行查看 历史
Sjianzhao 提交于 2021-04-06 18:34 . update
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;
/*
//类的最简单例子
typedef class student //类可以封装对象,类是C++的灵魂
{
private: //私有域
public: //公有域 类里面 不带循环的函数 会被 隐含转换成 inline类型 的函数,成员函数的代码不占用对象内存空间
int hour;
int min;
int sec;
} str; //st等价于str
int main(void)
{
str temp;
cin >> temp.hour;
cin >> temp.min >> temp.sec;
cout << temp.hour << endl << temp.min << endl << temp.sec << endl;
cout << "class size = " << sizeof(str) << endl;
system("pause");
return 0;
}
*/
/**
//类里面的数据成员默认为私有的
typedef class time
{
public:
int hour;
int min;
int sec;
}tms;
int set_time(tms &p1)
{
cin >> p1.hour >> p1.min >> p1.sec;
return 0;
}
int out_time(tms &p1)
{
cout << p1.hour << endl << p1.min << endl << p1.sec << endl;
return 0;
}
int main(void)
{
tms q1, q2;
set_time(q1);
out_time(q1);
system("pause");
return 0;
}
*/
/*
typedef class time //创建的类不占用内存,但是定义类变量就要占用内存
{
private:
int hour = 0;
int min = 0;
int sec = 0;
public:
int set_time();
int out_time();
}tms;
int tms::set_time(void)
{
cin >> hour >> min >> sec;
return 0;
}
int tms::out_time()
{
tms p;
cout <<p.hour << ":" << p.min << ":" << p.sec << endl;
return 0;
}
int main(void)
{
tms s1;
s1.out_time();
s1.set_time(); s1.out_time();
system("pause");
return 0;
}
*/
/*
//求最大值
typedef class jz_std
{
private:
int jz_table[10];
int jz_max;
public:
int set_value();
int max_value();
int out_value();
}jz_st;
int jz_st::set_value(void)
{
for(int i = 0;i < 10; i++)
cin >> jz_table[i];
return 0;
}
int jz_st::max_value(void)
{
for (int i = 0; i < 10; i++)
if (jz_table[i] > jz_max)
jz_max = jz_table[i];
return 0;
}
int jz_st::out_value(void)
{
cout << jz_max << endl;
return 0;
}
int main(void)
{
jz_st q1;
q1.set_value();
q1.max_value();
q1.out_value();
system("pause");
return 0;
}
*/
/*
//类也是一种ADT
typedef class jz_std
{
private:
public:
};
*/
typedef class MyClass_1
{
private:
int hour;
int min;
int sec;
public:
int set_MyClass();
int out_MyClass();
}jz_st_1;
int jz_st_1::set_MyClass(void)
{
cin >> hour >> min >> sec;
return 0;
}
int jz_st_1::out_MyClass(void)
{
cout << hour << min << sec << endl;
return 0;
}
typedef class MyClass_2
{
private:
int hour;
int min;
int sec;
public:
int set_MyClass()
{
cin >> hour >> min >> sec;
return 0;
}
int out_MyClass()
{
cout << hour << ":" << min <<":" << sec << endl;
return 0;
}
}jz_st_2;
typedef class MyClass_3
{
private:
float lenth;
float wid;
float height;
public:
int set_value();
int computer_tiji();
int out_three_tiji();
}jz_st_3;
int jz_st_3::set_value(void)
{
cout << "请输入第1个长宽高:";
cin >> lenth >> wid >> height;
out_three_tiji();
cout << "请输入第2个长宽高:";
cin >> lenth >> wid >> height;
out_three_tiji();
cout << "请输入第3个长宽高:";
cin >> lenth >> wid >> height;
out_three_tiji();
return 0;
}
int jz_st_3::computer_tiji(void)
{
float V;
V = lenth * wid * height;
return (int)V;
}
int jz_st_3::out_three_tiji()
{
cout << "当前长柱体体积为 : " << computer_tiji() << endl;
return 0;
}
int main(void)
{
/*
// 1 and 3 and 5
jz_st_1 p1;
p1.set_MyClass();
p1.out_MyClass();
// 2
jz_st_2 p2;
p2.set_MyClass();
p2.out_MyClass();
cout << "jz_st_2 type = " << sizeof(jz_st_2) << endl;
*/
// 6
jz_st_3 p3;
p3.set_value();
system("pause");
return 0;
}
C++
1
https://gitee.com/Sjianzhao/Cplusplus.git
git@gitee.com:Sjianzhao/Cplusplus.git
Sjianzhao
Cplusplus
C++学习
master

搜索帮助