1、类:
(1)使用:
#include <iostream>
using namespace std;
class family //类名
{
public: //共有成员
int son;
bool father;
char mother;
void fun();
private: //私有成员
void room();
protected: //保护成员
int l[10];
};
void family::room() //类的成员函数功能
{
son = 1; //成员变量
for (int i=0;i<10;i++)
{
l[i] = i; //成员数组
}
cout<<"我是铁臂阿童木"<<" "<<l[5]<<"号"<<endl;
}
void family::fun() //类的成员函数功能
{
room();
}
int main()
{ //创建类的对象
family a; //无法使用protected和private的变量和函数
a.fun();
a.son = 1;
a.father = false;
a.mother = a.son;
cout<<a.son<<" "<<a.mother<<" "<<a.father;
return 0;
}
运行结果:
(2)public、private、protected区别:
来自:https://blog.csdn.net/vanturman/article/details/79393317
public
:可以被该类中的函数、子类的函数、其友元函数访问,也可以由该类的对象访问。
private
:只能由该类中的函数、其友元函数访问,不能被任何其他访问,该类的对象也不能访问。
protected
:可以被该类中的函数、子类的函数、以及其友元函数访问,但不能被该类的对象访问。
(3)概念:
以下资料来自菜鸟教程。
友元函数:
类的友元函数是定义在类外部,但有权访问类的所有私有(private
)成员和保护(protected
)成员。
this指针:
存在于类中,指向类本身,比如qt
中常用的this->setStyleSheet()
。
内联函数:
内联函数是指用inline
关键字修饰的函数,通常与类一起使用。如果一个函数是内联的,那么在编译时,编译器会把该函数的代码副本放置在每个调用该函数的地方。对内联函数进行任何修改,都需要重新编译函数的所有客户端,因为编译器需要重新更换一次所有的代码,否则将会继续使用旧的函数。
静态成员:
使用static
关键字来把类成员定义为静态的。当我们声明类的成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本。
(4)类的实例化传参:
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
int no;
string name;
student(int Sno,string Sname)
{
no=Sno;name=Sname;
};
};
int main()
{
student yang(100,"asdsa"); //实例化传参
cout<<yang.no<<" "<<yang.name;
return 0;
}
或者:
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
int no;
string name;
student(int Sno,string Sname):no(Sno),name(Sname){};
};
int main()
{
student yang(100,"asdsa"); //实例化传参
cout<<yang.no<<" "<<yang.name;
return 0;
}
(5)类使用自身作为函数参数:
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
int no;
string name;
student(int Sno,string Sname):no(Sno),name(Sname){};
void j(student a,int &b) //类自身作为函数参数
{
b+= a.no;
}
};
int main()
{
student yang(100,"asdsa");
int b =11;
yang.j(yang,b);
cout<<yang.no<<" "<<yang.name<<" "<<b;
return 0;
}
其实就等同于下面的:
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
int no;
string name;
student(int Sno,string Sname):no(Sno),name(Sname){};
void j(int &b)
{
b+= no;
}
};
int main()
{
student yang(100,"asdsa");
int b =11;
yang.j(b);
cout<<yang.no<<" "<<yang.name<<" "<<b;
return 0;
}
2、结构体:
(1)使用:
#include <iostream>
using namespace std;
struct
{
int son; //结构体成员
double mother;
long father;
} family;
int main()
{
family.father = 1;
family.mother = 2.2;
family.son = 3;
cout<<family.father<<" "<<family.mother<<" "<<family.son;
return 0;
}
输出结果:
(2)结构体指针:
外部指针:
#include <iostream>
using namespace std;
struct family
{
int son;
int mother;
int father;
} family1 = { 3,29,27 };
int main()
{
cout<<family1.son<<" "<<family1.mother<<" "<<family1.father<<endl;
struct family *pst = &family1 ;
//也可以 (*pst).father = 30;
// . 的优先级高于 * ,所以要加括号
pst->father = 2;
pst->mother = 27;
pst->son = 25;
cout<<pst->father<<" "<<pst->mother<<" "<<pst->son<<endl;
return 0;
}
或者写成:
struct family
{
int son;
int mother;
int father;
} family1 = { 3,29,27 }, *pst = &family1;;
运行结果:
内部指针:
#include <iostream>
#include <string.h>
using namespace std;
struct family
{
char *son;
int mother;
int father;
};
int main()
{
struct family fam1;
fam1.mother = 11;
fam1.father = 12;
fam1.son = (char*)malloc(sizeof(char));
strcpy(fam1.son,"哈哈");
cout<<fam1.mother<<" "<<fam1.son<<" "<<fam1.father;
return 0;
}
运行结果:
(3)结构体中的函数和继承:
#include <iostream>
using namespace std;
struct a{
int out(){cout<<"我是结构体";} //结构体中的函数
};
struct b:public a //结构体中的继承
{
};
int main()
{
b b1;
b1.out();
return 0;
}
输出结果:
3、类和结构体的区别:
资料来自:https://www.cnblogs.com/ogaligong/p/12372774.html
区别:
结构体(sturct
)是一种值类型,而类(class
)是引用类型。区别在于复制方式,值类型的数据是值赋值,引用类型的数据是引用复制。
结构体使用栈存储(Stack Allocation
),而类使用堆存储(Heap Allocation
)。
栈的空间相对较小.但是存储在栈中的数据访问效率相对较高。
堆的空间相对较大.但是存储在堆中的数据的访问效率相对较低
结构体使用完之后就自动解除内存分配,类实例有垃圾回收机制来保证内存的回收处理。
如何选择结构体还是类:
(1) 堆栈的空间有限,对于大量的逻辑的对象,创建类要比创建结构好一些。
(2) 结构体表示如点、矩形和颜色这样的轻量对象,例如,如果声明一个含有 1000 个点对象的数组,则将为引用每个对象分配附加的内存。在此情况下,结构体的成本较低。
(3) 在表现抽象和多级别的对象层次时,类是最好的选择,因为结构体不支持继承。
(4) 大多数情况下该类型只是一些数据时,结构体时最佳的选择。