C 过渡 C++ 1

1,C++

GCC 编译C++兼容C代码

#include <stdio.h>
#include <string.h>

struct student
{
        int ID;
        char name[100];
};

int main()
{
        struct student st;
        st.ID = 10;
        strcpy(st.name,"tom");
        printf("%d,%s\n",st.ID,st.name);
        return  0;
}

编译运行
[email protected]:~/file$ g++ main.cpp && ./a.out
10,tom

C++ 结构体,构造函数,析构函数

[email protected]:~/file$ cat main.cpp 
#include <stdio.h>
#include <string.h>

struct student
{
	int ID;
	char name[100];
	void set_id(int id)	//C++中结构体支持函数了
	{
		ID = id;
	}

	void set_name(const char *name)
	{
		strcpy(this->name,name);
	}

	//构造函数            
	student(int ID,const char *name)
	{
		this -> ID = ID;
		strcpy(this ->name,name);
		printf("结构体的构造函数\n");
	}
	//析构函数            
	~student()
	{
		printf("结构体的析构函数\n");
	}

};

int main()
{
	student st(99,"忘情水");
	printf("%d,%s\n",st.ID,st.name);

	return  0;
}

[email protected]:~/file$ 
[email protected]:~/file$ g++ main.cpp && ./a.out
结构体的构造函数
99,忘情水
结构体的析构函数

C++结构体的指针

[email protected]:~/file$ cat main.cpp 
#include <stdio.h>
#include <string.h>

struct student
{
	int ID;
	char name[100];
	void set_id(int id)	//C++中结构体支持函数了
	{
		ID = id;
	}

	void set_name(const char *name)
	{
		strcpy(this->name,name);
	}

	//构造函数            
	student(int ID,const char *name)
	{
		this -> ID = ID;
		strcpy(this ->name,name);
		printf("结构体的构造函数\n");
	}
	//析构函数            
	~student()
	{
		printf("结构体的析构函数\n");
	}

};

int main()
{
	student *st = new student(99,"我是C++结构体指针");
	printf("%d,%s\n",st->ID,st->name);
	delete st;

	return  0;
}

[email protected]:~/file$ g++ main.cpp && ./a.out
结构体的构造函数
99,我是C++结构体指针
结构体的析构函数
[email protected]:~/file$

C++ 结构体 转类

[email protected]:~/file$ cat main.cpp 
#include <stdio.h>
#include <string.h>

class student
{
public:
	int ID;
	char name[100];
	void set_id(int id)	//C++中结构体支持函数了
	{
		ID = id;
	}

	void set_money(int money)
	{
		this->money = money;
	}
	int get_money()
	{
		return money;
	}
	void set_name(const char *name)
	{
		strcpy(this->name,name);
	}

	//构造函数            
	student(int ID,const char *name)
	{
		this -> ID = ID;
		strcpy(this ->name,name);
		printf("结构体的构造函数\n");
	}
	//析构函数            
	~student()
	{
		printf("结构体的析构函数\n");
	}

private:
	int money;//在外面是不允许直接访问的
};

int main()
{
	student *st = new student(99,"我是C++结构体指针");
	st->set_money(9999);
	int money = st->get_money();
	printf("ID  = %d,name = %s,money = %d\n",st->ID,st->name,money);
	delete st;

	return  0;
}

[email protected]:~/file$ g++ main.cpp && ./a.out
结构体的构造函数
ID  = 99,name = 我是C++结构体指针,money = 9999
结构体的析构函数
[email protected]:~/file$

C++ 类文件分开写

[email protected]:~/file$ vim main.cpp 
#include <stdio.h>
#include <string.h>
#include "student.h"
int main()
{
	student *st = new student(1,"Linus Torvalds");
	st->set_money(999999);
	printf("ID  = %d\n",st->ID);
	printf("name = %s\n",st->name);
	printf("money = %d\n",st->get_money());
	delete st;

	return  0;
}

[email protected]:~/file$ vim student.h 
#ifndef STUDENT_H
#define STUDENT_H
class student
{
public:
	int ID;
	char name[100];
	void set_id(int id);	//C++中结构体支持函数了
	void set_money(int money);
	int get_money();
	void set_name(const char *name);
	student(int ID,const char *name);
	~student();
private:
	int money;//在外面是不允许直接访问的
};
#endif //STUDENT_H
[email protected]:~/file$ 

[email protected]:~/file$ vim student.cpp 
#include <stdio.h>
#include <string.h>
#include "student.h"

void student::set_id(int id)	//C++中结构体支持函数了
{
	ID = id;
}

void student:: set_money(int money)
{
	this->money = money;
}
int student:: get_money()
{
	return money;
}
void student:: set_name(const char *name)
{
	strcpy(this->name,name);
}

//构造函数            
student::student(int ID,const char *name)
{
	this -> ID = ID;
	strcpy(this ->name,name);
	printf("构造函数被调用\n");
}
//析构函数            
student::~student()
{
	printf("析构函数被调用\n");
}

编译执行:
[email protected]:~/file$  g++ main.cpp student.cpp && ./a.out 
构造函数被调用
ID  = 1
name = Linus Torvalds
money = 999999
析构函数被调用

c++实现在主函数之前执行代码

#include <stdio.h>
class first
{
public: 
        first()//构造函数,与类名相同。
        {
                printf("myclass开始 \n");
        }
        ~first()//析构函数,与类名相同。
        {
                printf("myclass结束 \n");
        }
        
};

first haha;//定义一个类

int main()
{
        printf("main 开始 \n");
        printf("main 结束 \n");
        return  0;
}

[email protected]:~/class$ g++ main.cpp && ./a.out 
myclass开始 
main 开始 
main 结束 
myclass结束
时间: 2024-08-28 15:00:56

C 过渡 C++ 1的相关文章

css3变形 过渡 动画

CSS3 变形/变换 相关属性 transform 设置或检索对象的检索(none 2D 3D) transform-origin:设置或检索对象以某个原点进行检索 transform-style: flat(默认)指定子元素位于次元素所在平面内/preserve-3d 指定子元素定位在三维空间内 perspective: 长度单位 指定观察者距离平面的距离 perspective-origin 指定观察者的位置 left/right/center backface-visibialbe: vi

Unity中的平滑过渡方法了解

只是刚入门,通过代码以及文档比较一下异同点,以后遇到问题方便选择. Mathf.Lerp // UnityEngine.Mathf /// <summary> /// <para>Linearly interpolates between a and b by t.</para> /// </summary> /// <param name="a">The start value.</param> /// <

CSS3之过渡及2D变换

transition过渡 transition-duration:; 运动时间transition-delay:; 延迟时间transition-timing-function:; 运动形式 ease 逐渐变慢 (默认) linear 匀速 ease-in 加速 ease-out 减速 ease-in-out 先加速后减速 cubic-bezier 贝塞尔曲线(x1,y1,x2,y2) 通过控制曲线走势来改变运动效果 注:多样式同时进行过渡(需要异步过渡 则需在完成时间后再加一个参数:延迟时间)

CSS3学习(CSS3过渡、CSS3动画)

CSS3过渡:transition属性--专门应对颜色.长度.宽度.位置等变化的过渡 通过CSS3,我们可以在不使用Flash和JavaScript的情况下,为当前某元素从某样式改变为某样式的时候添加过渡效果.我们仅仅使用到了一个transition属性,专门来写过渡从一个样式到另一个样式过渡时所花费的时间,以秒为单位.若时长不规定,默认为0,即没有过渡时间.在使用这个过渡效果的时候,我们使用了类似于超链接的l(link).v(visited).h(hover).a(active)样式的控制.此

css3中的变形(transform)、过渡(transtion)、动画(animation)

Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix.下面我们一起来看看CSS3中transform的旋转rotate.扭曲skew.缩放scale和移动translate具体如何实现,老样子,我们就从transform的语法开始吧.是构成transtion和animation的基础. 语法: transform : none | <transform-func

一组网页边栏过渡动画,创意无限!【附源码下载】

今天我们想与大家分享另一套过渡效果.这一次,我们将探讨如何实现侧边栏的过渡动画,就像我们已经在多级推出菜单中使用的.我们的想法是,以细微的 过渡动画显示一些隐藏的侧边栏,其余的内容也是.通常侧边栏滑入,把其他内容推到一边.这个可过程中可以加入很多微妙而奇特的效果,而今天这篇文章能够给 你一些启示. 温馨提示:为保证最佳的效果,请在 IE10+.Chrome.Firefox 和 Safari 等现代浏览器中浏览. 立即下载      在线演示 因为我们希望能够在一个页面上展现所有的效果,因此我们示

创意无限!一组网页边栏过渡动画【附源码下载】

今天我们想与大家分享另一套过渡效果.这一次,我们将探讨如何实现侧边栏的过渡动画,就像我们已经在多级推出菜单中使用的.我们的想法是,以细微的过渡动画显示一些隐藏的侧边栏,其余的内容也是.通常侧边栏滑入,把其他内容推到一边.这个可过程中可以加入很多微妙而奇特的效果,而今天这篇文章能够给你一些启示. 温馨提示:为保证最佳的效果,请在 IE10+.Chrome.Firefox 和 Safari 等现代浏览器中浏览. 立即下载      在线演示 因为我们希望能够在一个页面上展现所有的效果,因此我们示例的

CSS transition 过渡 详解

transition 过渡 IE10.Firefox.Chrome.Opera 支持 transition 属性. Safari 需要前缀 -webkit-. Chrome 25 以及更早版本需要前缀 -webkit-. IE9 以及更早版本不支持 transition 属性. 过渡属性 transition-property:规定应用过渡效果的 CSS 属性的名称(当指定的CSS属性改变时,过渡效果开始),其默认值为 all . transition-duration:规定完成过渡效果需要的时

【CSS3】transition过渡

一.transition CSS3的过渡功能就像是一种黄油,可以让CSS的一些变化变得平滑.因为原生的CSS过渡在客户端需要处理的资源要比用JavaScript和Flash少的多,所以才会更平滑. transition的属性 属性可以分开写,也可以放在一起写,比如下面的代码,图片的宽高本来都是15px,想要让它1秒的时间内过渡到宽高为450px,通过:hover来触发,那么代码就可以如下: img{ height:15px; width:15px; transition: 1s 1s heigh

CSS3 过渡

通过 CSS3,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果. 请把鼠标移动到下面的元素上: 浏览器支持 属性 浏览器支持 transition           Internet Explorer 10.Firefox.Chrome 以及 Opera 支持 transition 属性. Safari 需要前缀 -webkit-. 注释:Internet Explorer 9 以及更早的版本,不支持 transition