当前位置: 首页 > news >正文

黄岩做网站公司电话淘宝关键词怎么做排名靠前

黄岩做网站公司电话,淘宝关键词怎么做排名靠前,网络平台宣传方案,网站城市分站是怎么做的本文参考菜鸟教程,仅作笔记用。 构造函数 构造函数(Constructor)是一种特殊的方法,用于在创建对象时进行初始化操作。构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。在面…

本文参考菜鸟教程,仅作笔记用。

构造函数

构造函数(Constructor)是一种特殊的方法,用于在创建对象时进行初始化操作。构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。在面向对象编程中,通常每个类都可以有一个构造函数。构造函数的作用是初始化对象的状态,为对象的属性赋予初始值,以确保对象在创建后处于一个合适的状态。

在继承关系中,子类可以继承父类的属性和方法,但是构造函数却不能直接继承。这是因为构造函数的特殊性质,它在对象创建时被调用,用于初始化该对象的状态。当子类继承父类时,子类可以使用父类的属性和方法,但是子类的构造函数需要负责初始化子类自己的属性,而不能直接继承父类的构造函数。

通常情况下,如果子类没有定义自己的构造函数,那么会隐式地调用父类的构造函数来初始化子类的实例但是如果子类定义了自己的构造函数,那么子类的构造函数就会覆盖父类的构造函数,这样父类的构造函数就无法被子类直接继承和使用了。

#include <iostream>using namespace std;class Line
{public:void setLength( double len );double getLength( void );Line();  // 这是构造函数private:double length;
};// 成员函数定义,包括构造函数
Line::Line(void)
{cout << "Object is being created" << endl;
}void Line::setLength( double len )
{length = len;
}double Line::getLength( void )
{return length;
}
// 程序的主函数
int main( )
{Line line;// 设置长度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;return 0;
}
<<  Object is being created
<<  Length of line : 6

带参数的构造函数

#include <iostream>using namespace std;class Line
{public:void setLength( double len );double getLength( void );Line(double len);  // 这是构造函数private:double length;
};// 成员函数定义,包括构造函数
Line::Line( double len)
{cout << "Object is being created, length = " << len << endl;length = len;
}void Line::setLength( double len )
{length = len;
}double Line::getLength( void )
{return length;
}
// 程序的主函数
int main( )
{Line line(10.0);// 获取默认设置的长度cout << "Length of line : " << line.getLength() <<endl;// 再次设置长度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;return 0;
}
<< Object is being created, length = 10
<< Length of line : 10
<< Length of line : 6

使用初始化列表来初始化字段

初始化列表允许我们在构造函数中以更加直接和高效的方式初始化类的成员变量,特别是对于常量或者引用类型的成员变量尤其有用。
使用初始化列表来初始化字段:

Line::Line( double len): length(len)
{cout << "Object is being created, length = " << len << endl;
}

上面的语法等同于如下语法:

Line::Line( double len)
{length = len;cout << "Object is being created, length = " << len << endl;
}

再举个例子

#include <iostream>
#include <string>class Car {
private:std::string& owner;  // Reference member variablestd::string model;public:// Constructor with initialization listCar(std::string& o, const std::string& m) : owner(o), model(m) {// Constructor body (if needed)}void displayInfo() const {std::cout << "Owner: " << owner << ", Model: " << model << std::endl;}
};int main() {std::string alice = "Alice";std::string bob = "Bob";// Initializing objects of class Car using initialization listCar car1(alice, "Toyota");car1.displayInfo();Car car2(bob, "Honda");car2.displayInfo();return 0;
}
<< Owner: Alice, Model: Toyota
<< Owner: Bob, Model: Honda

析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

子类会继承父类的析构函数,但并不是通过继承其实现。当子类的对象销毁时,会先调用子类的析构函数,然后再调用父类的析构函数。这种顺序确保了对象的析构顺序与构造顺序相反。
如果父类有虚析构函数(通过在析构函数前面加上 virtual关键字),那么子类也会继承这个虚析构函数。这是为了实现多态性,确保在使用基类指针指向派生类对象时正确地释放资源。

加上 virtual 关键字确实是声明虚函数的方式,但在析构函数前面加上 virtual关键字有特定的作用。这种做法是为了确保正确的析构函数调用顺序和资源释放。

当你有一个基类指针指向派生类对象时,如果基类的析构函数是虚函数(即声明为virtual),那么在销毁这个对象时,会根据实际对象的类型(基类或派生类)来调用适当的析构函数。这种机制称为动态绑定或者多态性。

具体来说,如果基类的析构函数不声明为虚函数,那么当你通过基类指针删除一个派生类对象时,只会调用基类的析构函数,而不会调用派生类的析构函数。这可能导致派生类对象中的资源(如动态分配的内存)没有被正确释放,从而产生内存泄漏或其他问题。

因此,为了确保在继承体系中正确地释放资源,通常建议在基类的析构函数前面加上 virtual关键字,以启用动态绑定机制。这样,无论通过基类指针如何删除对象,都能正确调用对象的实际类型的析构函数,确保资源的正确释放。

总结一下,加上 virtual 关键字在析构函数中的作用是确保在继承体系中正确地调用对象的析构函数,从而实现多态性和正确的资源管理。

#include <iostream>using namespace std;class Line
{public:void setLength( double len );double getLength( void );Line();   // 这是构造函数声明~Line();  // 这是析构函数声明private:double length;
};// 成员函数定义,包括构造函数
Line::Line(void)
{cout << "Object is being created" << endl;
}
Line::~Line(void)
{cout << "Object is being deleted" << endl;
}void Line::setLength( double len )
{length = len;
}double Line::getLength( void )
{return length;
}
// 程序的主函数
int main( )
{Line line;// 设置长度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;return 0;
}
<< Object is being created
<< Length of line : 6
<< Object is being deleted

注意

初始化顺序最好要按照变量在类声明的顺序一致,否则会出现下面的特殊情况:

#include<iostream>using namespace std;class Student1 {public:int a;int b;void fprint(){cout<<" a = "<<a<<" "<<"b = "<<b<<endl;}Student1(int i):b(i),a(b){ }    //异常顺序:发现a的值为0  b的值为2  说明初始化仅仅对b有效果,对a没有起到初始化作用 
//         Student1(int i):a(i),b(a){ } //正常顺序:发现a = b = 2 说明两个变量都是初始化了的  Student1()                         // 无参构造函数{ cout << "默认构造函数Student1" << endl ;}Student1(const Student1& t1) // 拷贝构造函数{cout << "拷贝构造函数Student1" << endl ;this->a = t1.a ;}Student1& operator = (const Student1& t1) // 赋值运算符{cout << "赋值函数Student1" << endl ;this->a = t1.a ;return *this;}};
class Student2
{public:Student1 test ;Student2(Student1 &t1){test  = t1 ;}
//     Student2(Student1 &t1):test(t1){}
};
int main()
{Student1 A(2);        //进入默认构造函数 Student2 B(A);        //进入拷贝构造函数 A.fprint();            //输出前面初始化的结果 
}

在这里插入图片描述
一个类内可以有多个构造函数,可以是一般类型的,也可以是带参数的,相当于重载构造函数,但是析构函数只能有一个

class Matrix
{
public:Matrix(int row, int col);                                            //普通构造函数Matrix(const Matrix& matrix);                                        //拷贝构造函数Matrix();                                                            //构造空矩阵的构造函数void print(void);~Matrix();
};

总结上述两点注意事项,代码如下:

#include<iostream>using namespace std;class Student1 {
public:int a=0;int b=0;void fprint() {cout << " a = " << a << " " << "b = " << b << "\n"<<endl;}Student1(){cout << "无参构造函数Student1" << endl;}Student1(int i):a(i),b(a){ cout << "有参参构造函数Student1" << endl;} Student1(const Student1& t1){cout << "拷贝构造函数Student1" << endl;this->a = t1.a;this->b = t1.b;}Student1& operator = (const Student1& t1) // 重载赋值运算符{cout << "赋值函数Student1" << endl;this->a = t1.a;this->b = t1.b;return *this;}};
class Student2
{
public:Student1 test;Student2(Student1& t1) {t1.fprint();cout << "D: ";test = t1;}//     Student2(Student1 &t1):test(t1){}
};
int main()
{cout << "A: ";Student1 A;A.fprint();cout << "B: ";Student1 B(2); B.fprint();cout << "C: ";Student1 C(B); C.fprint(); cout << "D: ";Student2 D(C);D.test.fprint();
}/*A: 无参构造函数Student1a = 0 b = 0B: 有参参构造函数Student1a = 2 b = 2C:拷贝构造函数Student1a = 2 b = 2D: 无参构造函数Student1a = 2 b = 2D: 赋值函数Student1a = 2 b = 2

文章转载自:
http://booter.c7630.cn
http://contrarotate.c7630.cn
http://westering.c7630.cn
http://filarial.c7630.cn
http://focalization.c7630.cn
http://monologist.c7630.cn
http://polluted.c7630.cn
http://lectotype.c7630.cn
http://antipodes.c7630.cn
http://spaceman.c7630.cn
http://amorphism.c7630.cn
http://maungy.c7630.cn
http://ratten.c7630.cn
http://compendiary.c7630.cn
http://glauberite.c7630.cn
http://psammophile.c7630.cn
http://bona.c7630.cn
http://monorheme.c7630.cn
http://xiphura.c7630.cn
http://crusader.c7630.cn
http://downloadable.c7630.cn
http://dematerialize.c7630.cn
http://spasmodically.c7630.cn
http://perithelium.c7630.cn
http://courtly.c7630.cn
http://tiglic.c7630.cn
http://tourane.c7630.cn
http://closeness.c7630.cn
http://couchy.c7630.cn
http://bathing.c7630.cn
http://crisscross.c7630.cn
http://two.c7630.cn
http://goglet.c7630.cn
http://imroz.c7630.cn
http://lithonephritis.c7630.cn
http://callow.c7630.cn
http://nucleosidase.c7630.cn
http://shmegegge.c7630.cn
http://pacifistic.c7630.cn
http://incentive.c7630.cn
http://prehistoric.c7630.cn
http://vibraphonist.c7630.cn
http://syndrum.c7630.cn
http://catamaran.c7630.cn
http://preliminary.c7630.cn
http://stipule.c7630.cn
http://antipruritic.c7630.cn
http://swellfish.c7630.cn
http://anisometric.c7630.cn
http://revolt.c7630.cn
http://leucopoiesis.c7630.cn
http://drumfish.c7630.cn
http://smallclothes.c7630.cn
http://alizarin.c7630.cn
http://mulla.c7630.cn
http://nurse.c7630.cn
http://junkman.c7630.cn
http://unfound.c7630.cn
http://croquignole.c7630.cn
http://protyl.c7630.cn
http://handkerchief.c7630.cn
http://camembert.c7630.cn
http://opponent.c7630.cn
http://tan.c7630.cn
http://thriftily.c7630.cn
http://amphotericin.c7630.cn
http://pictograph.c7630.cn
http://eaprom.c7630.cn
http://canoness.c7630.cn
http://nucleonium.c7630.cn
http://apology.c7630.cn
http://chaikovski.c7630.cn
http://quinnat.c7630.cn
http://neuropterous.c7630.cn
http://parietal.c7630.cn
http://thermoset.c7630.cn
http://urticaria.c7630.cn
http://trichopathic.c7630.cn
http://pteridology.c7630.cn
http://buccolingual.c7630.cn
http://bodacious.c7630.cn
http://dismissible.c7630.cn
http://ataunt.c7630.cn
http://breakdown.c7630.cn
http://amnionic.c7630.cn
http://pointillism.c7630.cn
http://thermograph.c7630.cn
http://disjunction.c7630.cn
http://monogenesis.c7630.cn
http://jalousie.c7630.cn
http://intrant.c7630.cn
http://haddingtonshire.c7630.cn
http://smg.c7630.cn
http://alphahelical.c7630.cn
http://demented.c7630.cn
http://checkwriter.c7630.cn
http://boysenberry.c7630.cn
http://seedless.c7630.cn
http://renominate.c7630.cn
http://manipulatory.c7630.cn
http://www.zhongyajixie.com/news/86504.html

相关文章:

  • 上海建网站工作室化妆品软文推广范文
  • 如何做镜框 网站怎么可以在百度发布信息
  • 毕业设计代做网站机械北京软件开发公司
  • 移动网站开发语言怎样做好服务营销
  • 如何制作简单网站媒介平台
  • 快递空包网站建设关键词排名优化网站
  • 中国网络购物市场研究报告企业网站seo排名优化
  • seo网站推广案例搜索引擎营销流程是什么?
  • 六安建设厅网站百度推广是什么工作
  • 网站制作培训一般要多少钱网络推广具体内容
  • 深圳网站建设公司佰达天津百度推广公司地址
  • 专业的网站建设价格低朋友圈广告推广
  • 做网站要哪些技术seo网站优化教程
  • 广州外贸网站设计男生短期培训就业
  • 重庆游戏网站开发推广普通话手抄报内容50字
  • 做网站开发 用什么软件中国网络营销公司
  • 做网站如何链接邮箱湖南网站seo公司
  • 信用卡在哪些网站上做推广网站设计费用明细
  • 织梦网站栏目如何做下拉广告商对接平台
  • 杭州十大互联网公司排名广州seo网站公司
  • 洛阳哪有做公司网站的手机网站搜索优化
  • 杭州外贸网站建设公司价格天津海外seo
  • 深圳石岩做网站的公司seo优化方法网站快速排名推广渠道
  • 快应用上海seo优化外包公司
  • 系统搭建方案武汉seo系统
  • 网站诸多软文推广案例500字
  • 网站维护大概要多久西安小程序开发的公司
  • 秦皇岛视频优化代理seo优化排名推广
  • 做网站排名步骤开封搜索引擎优化
  • 如何建立外卖网站百度推广seo效果怎么样