C++学习5访问限制、初始化、对象组合

访问限制

Private只有成员函数可以访问

注意:本笔记来自网易云课堂学习

例子:
#pragma once
class A
{
public:
 A();
 ~A();
 void g(A*);
 void set(int);
private:
 int i;
 int c;
};

 

#include "A.h"
#include<iostream>
using namespace std;

A::A()
{
}

A::~A()
{
}
void A::g(A * q)
{
 cout << q->i << endl;//同一个类的私有变量可以访问!
}
void A::set(int ii)
{
 i = ii;
}
#include "A.h"
int main()
{
 A *p = new A[10];//开辟一个空间10个对象
 for (int i = 0; i < 10;i++)
 {
  p[i].set(i);//这里p就相当于数组的名字,首地址
 }
 A b;
 b.set(100);//将b中的变量i设置成100
 p[0].g(&b);//将p[0]的i设置成b中的i的值。
 delete[] p;//有new就要有delete
 return 0;
}
==========

 

Friend是在类中声明,那个声明的那个函数或者类,可以访问这个类的私有成员。

 

struct X;

Struct Y(X*)//这里必须是X*不能是X c 不然就绕不出来了

{

}

Struct X{

Private:

Int I;

Public:

Friend void Y::f(X*)
}

 

Struct 和class 的区别

如果没有private ,那么struct默认为是public;而class 默认为是private;

 

如果类很简单只有几个数字,可以用struct,其他用class

 

初始化

初始化变量的值可以在初始化函数中

也可以在初始化函数名后加:给出值例如:

A():i(0){ }

 

还例如:

class point

{

private:

const float x,y;

point (float xa=0.0,ya=0.0):x(xa),y(ya)//构造函数

{ }

}

这样实际上,x,y的赋值要早于构造函数

 

建议初始化都在构造函数前,不然可能找不到初始化函数

 

 

 

 

 

对象组合

把已有的对象组合来实现软件重用

用对象来制造对象


定义的类person和currency是savingsaccount的成员变量

 

fully(上面例子中的直接的引用)

reference(使用指针)

 

要对包含的这两个类都初始化

构造函数SavingAccount::SavingAccuount(constchar* name,const* char*address,int cents)

m_saver(name),m_balance(0,cents){ }

 

如果把person类型的对象,定义在public中,那么就相当于外界可以访问内部的量。

例如:savingaccount A;//person中有set_name函数

A.person.set_name(“lily”)

阅读更多

C++ 组合 返回对象 初始化方式

05-15

1、声明一个Point类,有两个成员int m_x, m_y;rn2、声明另一个Circle类,他的一个数据成员为Point类的对象Point m_p,另一个数据成员为int m_radius;rn问题:rn1、我在初始化Circle类的对象时,是采用Circle c1(1, 1, 1);这种方式,还是Point p1(1, 1); Circle(p1, 1);这种方式,rn2、我想要得到一个Circle类的某个对象c1的m_p数据时,即直接返回m_p这个对象时,应该如何重载Point类中的拷贝构造函数及赋值构造函数rn下面贴代码,有点长、、、、困扰一天了,求大神解答、、、rn[code=c]/*point.h*/rn# ifndef _POINT_rn# define _POINT_rnrnclass Pointrnrnpublic:rn Point(int x = 0, int y = 0); //带默认参数的构造函数rn Point(Point &p); //拷贝构造函数rn void setPoint(int x, int y);rnrn void setX(int x);rn int getX()const;rnrn void setY(int y);rn int getY()const;rnrn void print()const;rn Point & operator =(const Point &p); //赋值运算符重载rn ~Point();rnrnprotected:rn int m_x;rn int m_y; rn;rnrn# endif[/code]rn[code=c]/*point.cpp*/rn# include rn# include "point.h"rnusing namespace std;rnrnPoint::Point(int x, int y):m_x(x), m_y(y)rnrn cout << "point constructed!" << endl;rnrnrnPoint::Point(Point &p)rnrn m_x = p.m_x;rn m_y = p.m_y;rn cout << "point copyconstructed!" << endl;rnrnrnvoid Point::setPoint(int x, int y)rnrn m_x = x;rn m_y = y;rnrnrnvoid Point::setX(int x)rnrn m_x = x;rnrnrnint Point::getX()constrnrn return m_x;rnrnrnvoid Point::setY(int y)rnrn m_y = y;rnrnrnint Point::getY()constrnrn return m_y;rnrnrnvoid Point::print()constrnrn cout << "function point" << endl;rn cout << "x = " << m_x << endl;rn cout << "y = " << m_y << endl;rnrnrnPoint & Point::operator =(const Point &p)rnrn if (this == &p)rn return *this;rn rn m_x = p.m_x;rn m_y = p.m_y;rn return *this;rnrnrnPoint::~Point()rnrn cout << "point destructor!" << endl;rn[/code]rn[code=c]/*circle.h*/rn# include "point.h"rnrn# ifndef _CIRCLE_rn# define _CIRCLE_rnrnclass Circlernrnpublic:rn //应该选如下的哪种方式为带默认参数的构造函数rn Circle(Point &p, int radius = 0);rn Circle(int x = 0, int y = 0, int radius = 0);rnrn void setCircle(int x, int y, int radius);rn void setCircle(Point &p, int radius);rnrn Point getPoint()const; //相应返回Point的对象m_p,不知应如何写rnrn void print()const;rnrn ~Circle();rnrnprivate:rn Point m_p;rn int m_radius;rn;rnrn# endif[/code]rn [code=c]/*circle.cpp*/rn# include rn# include "circle.h"rnusing namespace std;rnrnCircle::Circle(Point &p, int radius):m_p(p), m_radius(radius)rnrn cout << "circle 1 constructor!" << endl;rnrnrnCircle::Circle(int x, int y, int radius):m_p(x, y), m_radius(radius)rnrn cout << "circle 2 constructor!" << endl;rnrnrnvoid Circle::setCircle(int x, int y, int radius)rnrn m_p.setPoint(x, y);rn m_radius = radius;rnrnrnvoid Circle::setCircle(Point &p, int radius)rnrn m_p = p;rn m_radius = radius;rnrnrnPoint Circle::getPoint()constrnrn return m_p; //出错的位置,没有适当的复制构造函数rnrnrnvoid Circle::print()constrnrn cout << "function circle:" << endl;rn m_p.print();rn cout << "radius:" << m_radius << endl;rnrnrnCircle::~Circle()rnrn cout << "circle destructor!" << endl;rn[/code]rn[code=c]/*main.cpp*/rn# include rn# include "point.h"rn# include "circle.h"rnusing namespace std;rnrnint main()rnrn Point p1(0, 0);rn Circle c1(p1, 1);rn Point p2(c1.getPoint()); //想要的操作rnrn return 0;rn[/code]

没有更多推荐了,返回首页