c++基础的个人记录

@[kkkkkkkkkkaZZL
](记录 复习)(最终还是没有完成。。。。。)

->(暂无)静态


```cpp//  1
A (int x = 0 ,int y = 0):a1(x),a2(y){
		}  //  
 
2 **数组对象与数组指针**

```cpp
#include <iostream>
#include <fstream>

using namespace std;

class A{
	private:
		int a1,a2;
		double b2;
		string s1;
	public:
	   A (int x=0,int y=0):a1(x),a2(y){
	   }
	   ~A(){
	   };
	   void fprint()
	   {
	   	cout<<a1<<","<<a2<<"-----"<<endl;
	   }
};

int main()
{
    A aa[2]={ A(1,2),A(3,4)},*p; /// here
     aa[0].fprint();
     aa[1].fprint();
     //aa[0]->fprint();// wrong
     p=aa;
     p->fprint();
      
     // 1 (p+1)->fprint();
     // 2 
        p++;
        p->fprint();
	return 0;
}

3 作业杂项
3-1交换函数

void fun(int &a,int &b)
{
	int t;
	t=a;
	a=b;
	b=t;
}

3-2 三种继承的区别
from----https://zhidao.baidu.com/question/297018917.html?qbl=relate_question_1&word=%CB%BD%D3%D0%BC%CC%B3%D0%BA%CD%B1%A3%BB%A4%BC%CC%B3%D0%B5%C4%C7%F8%B1%F0

公有继承时基类中各成员属性保持不变,基类中private成员被隐藏。派生类的成员只能访问基类中的public/protected成员,而不能访问private成员;派生类的对象只能访问基类中的public成员。
私有继承时基类中各成员属性均变copy为private,并且基类中private成员被隐藏。派生类的zhidao成员也只能访问基类中的public/protected成员,而不能访问private成员;派生类的对象不能访问基类中的任何的成员。
保护继承时基类中各成员属性均变为protected,并且基类中private成员被隐藏。派生类的成员只能访问基类中的public/protected成员,而不能访问private成员;派生类的对象不能访问基类中的任何的成员。

3-3 利用构造函数创建对象,通常使用new

#include <iostream>
#include <string>

using namespace std;
///

class A{
	public:
		int a_1;
		
		A(int x=0):a_1(x){
		}
		~A(){
		}
	    void fun()
	    {
	    	cout<<a_1<<endl;
		}
};   
   
  
   
int main()
{
     A *p = new A(3);/heererererereere/
     p->fun();
    return 0;
}

3-4 构造函数重载。。。。。。。。

3-5 数组对象

 
class A{
	public:
		int a_1;
		 
		A(int x):a_1(x){
		}
		A(){
		};
		~A(){
		}
	    void fun()
	    {
	    	cout<<a_1<<endl;
		}
};   
int main()
{
      A *p = new A[3];
      p[0].fun();//  . 用 . 
      A xx[3] = { 1 ,  2  };
      xx[0].fun();
      xx[1].fun();
      xx[2].fun(); // 调用了不同的构造函数 
    return 0;
}

A a(8),*pp;
	  pp = &a;
	  pp->fun(); //指针
	  pp->fun();
	  pp->fun();


------------
class A{
	public:
		int a1;
		
		 A();//=====
		 ~A();//======
	 
		void geta1(int x)
		{
			a1 = x;
		}
		void fun()
		{
			cout<<a1<<endl;
		}
};
放外面=======
A::A(){   // here 
		 ;
		};
A::~A(){
		}	;

3-6 深拷贝 /// 未完成

差劲的例子

class A{
	public:
		int a1;
		 char * name;
		 A();//=====
		 ~A();//======
	     A(const A &a);
		void geta1(int x)
		{
			a1 = x;
		}
		void fun()
		{
			cout<<a1<<endl;
			cout<<name<<endl;
		}
};
A::A(){   // here
    name = new char(20);
		 ;
		};
A::A(const A &a)
{
	name = new char(20);
	memcpy(name,a.name,strlen(a.name));
	}	
A::~A(){
	delete name;
	name = NULL;
		}	;

更好的例子

#include <iostream>
using namespace std;
class CA
{
 public:
  CA(int b,char* cstr)
  {
   a=b;
   str=new char[b];
   strcpy(str,cstr);
  }
  CA(const CA& C)
  {
   a=C.a;
   str=new char[a]; //深拷贝
   if(str!=0)
    strcpy(str,C.str);
  }
  void Show()
  {
   cout<<str<<endl;
  }
  ~CA()
  {
   delete str;
  }
 private:
  int a;
  char *str;
};
/ 

int main()
{
 CA A(10,"Hello!");
 CA B=A;
 B.Show();
 return 0;
}
来自https://www.cnblogs.com/BlueTzar/articles/1223313.html

深拷贝例子

#include <iostream>
#include<bits/stdc++.h> 

using namespace std;
class A{
	private:
		int *a;
		string s;
	public:
	    A (string x,int *y){
	    	s = x;
	    	a = y;
		}
		A (const A&aa)
		{
		    	this->s=aa.s;
		    	this->a= new int(*aa.a);
		}	
		~A (){
			delete a;
			a = NULL;
		}
		void show()
		{
			cout<<s<<*a<<endl;
		}
		void chamgea(int c)
		{
			*a = c;
		}
}; 
int main()
{
	 int *y=new int(10);
	 string name = "xxxxxx";
	 A a1 = A (name,y);
	 A a2(a1);
	 a1.show();
	 a2.show();
	 a2.chamgea(6666);
	 a2.show();
	 
} 



bug----------------------------------------------------
--------------------------------------------------------
class A{
	private:
		int a_1;
		char *p;
	public:
	    A( int x,char *y){
	        a_1 = x;
	        p = y;
		}	
		~A(){
		};
		void show();
		void cha(char *y);
		
};
void A::cha(char *y)  
 {
 	p = y;
 }
void A::show()
{
    cout<<p<<endl;	
}
int main()
{
	char *p,a[3]="12",b[3]="99";
    p=a;
	A aa(3,p);
	aa.show();
	A bb(aa);
	bb.show();
	bb.cha(b);
	bb.show();
	aa.show();// 输出值相同?????   
	  
} 

3-7 友元
友元函数


 class A{
	private:
		int a_1;
	public:
	    A(int x=0):a_1(x){
		}	
		~A(){
		}
		void show();
		friend void show_(A&a);
};
void A::show()
{
	cout<<a_1<<endl;
}
void show_(A&a)
{
	cout<<a.a_1<<endl;
}
int main()
{
	A aa(2);
	aa.show();
	show_(aa);
} 

3-8类的组合
貌似不难,跳过

https://blog.csdn.net/weixin_40903417/article/details/86652949
 

3-9 常引用
4-1派生类的构造与析构函数
4-2 同名成员 访问声明
https://blog.csdn.net/nyist_yangguang/article/details/63685625

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值