C++笔记

#include <iostream>

using namespace std;

class Box
{
public://   public用来修饰下边的成员,可以是public(共有)  private(私有:只能在这个类里访问,其他地方访问不了) 
	double length;
	double width;
	double heigth;  //这三个是Box这个类的成员属性,还可以有方法 
	
	void show()//函数就是方法 
	{
		cout<<length<<endl;
		cout<<width<<endl;
		cout<<heigth<<endl;
	} 
	
};   //以上是构造一个类 


int main(void)
{
	Box box1;//这是声明类的对象
	Box box2;
	
	box1.length = 11.1;// 调用 属性 
	box1.width = 22.2;
	box1.heigth = 33.3; 
	box1.show();  //调用 方法 
	
	
	return 0;
}
#include <iostream>
#include <string.h>

using namespace std;
int main(void)
{
	string str1 = "GG";
	string str2 = "Bond";
	string str3 = str1+str2;//str1.append(str2);拼接 
	cout<<str3<<endl;
	cout<<str3.length()<<endl;//检测长度 
	cout<<str3.compare(str2)<<endl;//比较大小(并不是比较长短,是比较第一个字符),小返回负数,相等返回0,大返回正数 
	cout<<str3.size()<<endl; 
	
	return 0;
} 

#include <iostream>

using namespace std;

void swap(int &a,int &b) //此处可以和主函数中的a b同名是因为swap函数与主函数是两个函数所以可同名,若在主函数中定义引用则要不同名 
{
	int temp = a;
	a = b;
	b = temp; 
}

int main(void)
{
	int num = 6;
	int &num1 = num;
	
	int a=1,b=2;
	
	cout<<"num:"<<num<<endl;
	cout<<"num1:"<<num1<<endl;
	
	num1 = 66;
	
	cout<<"num:"<<num<<endl;
	cout<<"num1:"<<num1<<endl;
	
	cout<<"a:"<<a<<endl;
	cout<<"b:"<<b<<endl;
	swap(a,b);
	cout<<"a:"<<a<<endl;
	cout<<"b:"<<b<<endl;
	
	return 0;
}

/*   
	变量名是变量附属在内存中的标签, 引用可以看作是变量附属在内存中的第二个标签,
	因此可以通过原始变量名称或者引用来访问 变量的内容  具体的方式:  int &a = a; 


*/
#include <iostream>
#include <string.h>

using namespace std;

struct Student
{
	string name;
	int age;
	double score;
}s1;//第一种定义方式 


int main(void)
{
	//Student s1={"zhangsan",18,99.5} 第二种定义方式直接赋值 
	/*
	Student s1; 
	s1.
	第三种方式定义和赋值 
	*/ 
		
	s1.name="Zhangsan";
	s1.age=18;
	s1.score=99.5;
	
	cout<<"姓名:"<<s1.name<<",年龄:"<<s1.age<<",分数:"<<s1.score<<endl;	
		
	return 0;
} 
#include <iostream>

using namespace std;

int sum(int a,int b)
{
	return a+b;
}

void sum(int *a,int *b,int *sum)
{
	*sum = (*a) * (*b);
}//函数的重载,与返回值没关系,无论返回值是否类型相同,只要参数列表不一样,就是函数的重载
 //参数列表不同包括参数的类型不同,参数的数量不同,参数的顺序不同(比如一个是先char再int ,另一个是先Int再char) 

int main(void)
{
	int a,b,result;
	
	cin>>a;
	cin>>b;
	
	result = sum(a,b);
	cout<<result<<endl;
	
	sum(&a,&b,&result);
	cout<<result<<endl;
	return 0;
} 
/*
	通过函数的重载,可以在函数名相同的情况下通过传递的参数的不同来实现不同的函数功能; 


*/
#include <iostream>

using namespace std;

class Box
{
private:
	double length;
	double width;
	double height;
public:
	double volume;
	void setlength(double len);
	void setwidth(double wid);
	void setheight(double hei);
	void getvolume();
};

void Box::setlength(double len)
{
	this->length = len;
}

void Box::setwidth(double wid)
{
	this->width = wid;
}

void Box::setheight(double hei)
{
	this->height = hei;
}

void Box::getvolume()
{
	this->volume = length * width * height;
}

int main(void)
{
	double len,wid,hei,volume;
	Box box1;
	
	cin>>len;
	cin>>wid;
	cin>>hei;
	
	box1.setlength(len);
	box1.setwidth(wid);
	box1.setheight(hei);
	box1.getvolume();
	
	volume = box1.volume;
	
	cout<<"体积是:"<<volume<<endl; 
	
	return 0;
}

/*
	类中的成员和函数默认是私有的,需要声明public才能在外部使用,结构体中的成员默认是公共的不需要另外声明 
	public   公共的 
	private 私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的,只有类和友元函数可以访问私有成员 
	protected 保护成员变量和函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的 
*/
#include <iostream>

using namespace std;

class Box
{
private: 
	double length;
	double width;
	double height;
public:
	double volume;
	void setlength(double len);
	void setwidth(double wid);
	void setheight(double hei);
	void getvolume();
	
};

void Box::setlength(double len)  //  ::是C++里的作用域分解运算符,当类成员函数没有在类内定义,需要在类外定义时就需要在函数名前加上 类名:: 
{
	length = len; 
}

void Box::setwidth(double wid) //在外边定义的类成员函数,也可以访问私有的类属性,因为,虽然是在外边定义,但是声明在类的内部所以就相当于在类的内部定义 
{
	width = wid;
}

void Box::setheight(double hei)
{
	height = hei;  //若上边的参数也是height与类的属性名字一样,可以写成 this.height = height; 
}

void Box::getvolume()
{
	volume = length * height * width;
}

int main(void)
{
	double len,wid,hei;
	Box box1;
	
	cin>>len;
	cin>>wid;
	cin>>hei;
	
	box1.setlength(len);
	box1.setwidth(wid);
	box1.setheight(hei);
	box1.getvolume();
	
	cout<<"体积是:"<<box1.volume<<endl;
	
	return 0;
}
//类构造函数和析构函数
/*
	类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
	构造函数可用于为某些成员变量设置初始值 
	构造函数的名称与类的名称是完全相同的
	并且不会返回任何类型
	也不会返回 void。
*/ 
#include <iostream>

using namespace std;

class Box
{
private:
	double length;
	double width;
	double height;
public:
	double volume;
	
	//默认有一个空参构造函数(不用写也有),构造函数必须是public
	Box()  //空参构造函数,因为不返回任何类型,所以不用写返回值  
	{
		 
	}    //默认的空参构造函数就是这样,不写出来也默认有一个这样的  
		//前提:没写有参函数,只要写了有参函数 空参函数就用不了了,在声明对象时就只能使用有参函数
		//但是,只要把空参函数手动写出来就还能用空参函数 
	
	Box(double len,double wid,double hei)
	{
		this->length = len;
		this->width = wid;
		this->height = hei;
	} 
	
	~Box()
	{
		cout<<"对象被销毁"<<endl;
	} //析构函数 无参无返回值 在构造函数前加一个 ~   在构造的对象消亡时析构函数会自动调用  
		//如果 时通过 Box *box2 = new Box来构造的对象 析构函数在对象消亡时不会自动调用 
		//需要手动 delete box2来使对象消亡从而调用析构函数 
	
};



int main(void)
{
	double len,wid,hei,volume;
	Box box1;
	Box *box2 = new Box; 
	
	cin>>len;
	cin>>wid;
	cin>>hei;
	
	
	
	cout<<"体积是:"<<volume<<endl; 
	
	delete box2;
	return 0;
}

/*
	常量初始化时必须赋值或使用初始化列表赋值,引用 初始化时也需要复制或使用初始化列表赋值 
	常量和引用其实是在构造函数执行前赋值的
	class student
	{ 
	public :
		student ()
		protected:
		const int a;
		int &b;
	}
	
	student ::student (int i,int j)
	{
	a=i; //error
	b=j; //error
	}
	
	错误原因: 常量初始化时必须赋值或使用初始化列表赋值, 引用初始化也需要赋值或使用初始化列表赋值
	
	student ::student ( ):a(i),b(j)
	{
	
	} 
*/

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值