您的位置:首页 > 编程语言 > C语言/C++

面向对象 C++学习之我之过错

2017-10-17 21:02 260 查看
////标注的那行为关键行,是易错的地方

1.类内的函数声明:

注意这里是和C语言函数声明不一样的,不需要把形参名声明,只用形参类型,但在定义的时候就需要把形参名声明。

#include <iostream>
#include <cmath>

using namespace std;
double sum;
class Location
{
public :
Location(double ,double );
double getx(){return x;}
double gety() {return y;}
////  double distance(Location &); //注意这里是和C语言函数声明不一样的,不需要把形参名声明,只用形参类型
friend double distance(Location &,Location &);
private :
double  x;
double  y;
};
Location ::Location(double a,double b)
{
x=a;
y=b;
}
double Location ::distance(Location &p)
{
double dx=x-p.x;
double dy=y-p.y;
return (double)sqrt(dx*dx+dy*dy);
}
double distance(Location &p1,Location &p2)
{
double dx=p1.x-p2.x;
double dy=p1.y-p2.y;
return sqrt(dx*dx+dy*dy);
}
int  main()
{
Location a(-10,-20),b(-40,60);
cout<<a.getx()<<" "<<a.gety()<<endl;
cout<<b.getx()<<" "<<b.gety()<<endl;
cout<<a.distance(b)<<endl;
//distance(a,b);

return 0;
}

2虚函数与重载函数

详情看代码

#include <iostream>

using namespace std;
//bp->func1执行的是派生类中的成员函数,func1是虚函数
//bp->func2执行的是基类中的成员函数,失去了虚特性
//func3是错误的 既不是虚函数也不为重载函数
//bp->func4执行的是基类中的成员函数,普通的重载
class Base{
public :
virtual void func1();
virtual void func2();
virtual void func3();
void func4();
};
class Derived:public Base{
public :
virtual void func1();
void func2(int x);
//char func3()
void func4();
};
void Base::func1()
{
cout<<"B1"<<endl;
}
void Base::func2()
{
cout<<"B2"<<endl;
}
void Base::func3()
{
cout<<"B3"<<endl;
}
void Base::func4()
{
cout<<"B4"<<endl;
}
void Derived::func1()
{
cout<<"D1"<<endl;
}
void Derived::func2(int x)
{
cout<<"D2"<<endl;
}
void Derived::func4()
{
cout<<"D4"<<endl;
}
int main()
{
Base d1,*bp;
Derived d2;
bp=&d2;
bp->func1();
bp->func2();
bp->func4();
return 0;
}

3.转换构造与类型转换

#include <iostream>
using namespace std;
//输出结果的过程,1先寻找2个将类对象相加的运算符函数,程序中未有
//2 然后寻找到可以将类对象转换的转换函数,进行调用
//3 寻找将2个int型函数相加的运算符
//4 由于a3=10,隐式调用得到real=imag=5
class Complex{
public :
Complex() {};
Complex(int r,int i)
{   real=r;
imag=i;
}
Complex(int i)
{
real=imag=i/2;
}
operator int ()
{
return real+imag;
}
void print()
{
cout<<"real:"<<real<<"\t"<<"imag:"<<imag<<endl;
}
private :
int real;
int imag;
};
int main()
{
Complex a1(1,2),a2(3,4);
Complex a3;
a3=a1+a2;
a3.print();
return 0;
}

4.多重继承与虚函数

#include <iostream>
using namespace std;
//B1的派生路径:B1中的是虚函数,所以当指针指向obj3时,fun呈现虚特性,调用D::fun
//B2的派生路径:B2中的只是一般成员函数,所以此时是一个普通的重载函数,fun体现重载特性,调用B2

class Base1{
public :
virtual void fun()
{
cout<<"B1"<<endl;
}
};
class Base2{
public :
void fun()
{
cout<<"B2"<<endl;
}
};
class Derived:public Base1,public Base2{
public :
void fun()
{
cout<<"D"<<endl;
}
};
int main()
{
Base1 *ptr1;
Base2 *ptr2;
Derived obj3;
ptr1=&obj3;
ptr1->fun();

ptr2=&obj3;
ptr2->fun();
return 0;
}

5.运算符重载

友元函数重载运算符在声明的时候参数需要带取地址

成员函数重载运算符的时候则不需要

class Three{
public :
Three (int I1,int I2,int I3);
friend Three operator ++(Three &);  //友元前缀++
Three operator ++();         //成员前缀++
friend Three operator ++(Three &,int)  //友元后缀++
Three operator ++(int);         //成员后缀++
};

6.派生类的构造函数

格式 :派生类名(参数总表):基类名(参数表)

class Student{
public :
Student (int number1,string name1,float score1)
{  number=number1;
name=name1;
score=score1;
}
protected :
int number;
int name;
int score;
};
class UStudent :public Student
{
UStudent(int number1,string name1,float score1,string major1)
:Student(number1,name1,score1)
//定义派生类构造函数时,缀上要调用的基类的构造函数和参数
{
major=major1;
}
private :
string major;
};

7.析构函数

指针的析构函数在delete之后才会调用

对象的析构函数则是在程序运行完成之后

#include <iostream>

using namespace std;
class aClass
{
public:
aClass()
{total++;}
~aClass()
{total--;
cout<<"caonima"<<endl;}
int gettotal()
{
return total;
}
private :
static int total;
};
int aClass::total=0;
int main()
{
aClass o1,o2,o3;
cout<<o1.gettotal()<<"   O in "<<endl;
aClass *p;
p=new aClass;
cout<<o1.gettotal()<<"   O in a a"<<endl;
delete p;
cout<<o1.gettotal()<<"   o in a  2"<<endl;
return 0;
}

8.重载++符号

成员运算符重载  不需要加 &(引用号)

友元运算符重载   需要加&(引用号)

#include <iostream>

using namespace std;
class Coord
{
public :
Coord(int i=0,int j=0)
{
x=i;
y=j;
}
void print()
{
cout<<"x: "<<x<<" y:"<<y<<endl;
}
Coord operator++()
{
++x;
++y;
return *this;   //返回的是当前对象
}
friend Coord operator++(&ob)  //引用 如果没有引用则无法进行++
{
++ob.x;
++ob.y;
return ob;
}
private :
int x,y;
};
int main()
{
Coord ob(10,20);
ob.print();
++ob;
ob.print();
}

9.构造函数定义Complex (double r=0.0,double i=0.0);必须定义的时候加上=0.0,如果缺少的话,在定义一些对象的时候会因为赋随机值出错

#include <iostream>
using namespace std;
//对双目运算符而言,成员运算符重载函数的形参表中仅有一个参数
//它作为运算符的右操作数,做操作数是隐含的,是该类当前对象
//是通过this指针隐含的传给函数的
class Complex
{
public :
Complex (double r=0.0,double i=0.0);
void print();
Complex operator +(Complex c);
friend Complex operator-(Complex &a,Complex &b);
private :
double real;
double imag;
};
Complex::Complex(double r,double i)
{
real=r;

4000
imag=i;
}
Complex Complex::operator+(Complex c)
{
Complex temp;
temp.imag=imag+c.imag;
temp.real=real+c.real;
return temp;
}
Complex operator -(Complex &a,Complex &b)
{
Complex temp;
temp.imag=a.imag-b.imag;
temp.real=a.real-b.real;
return temp;
}
void Complex::print()
{
cout<<real<<"  "<<imag<<endl;
}
int main()
{
Complex  A1(2.1,4.1),A2(2.3,4.6),A3,A4;
A3=A1+A2;
A4=A2-A1;
A3.print();
A4.print();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: