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

面对对象编程总结(二)

2017-02-25 13:57 169 查看
1、static关键字的总结

static关键字:

对于特定类型的全体对象而言,有时候可能需要访问一个全局的变量,比如说统计某种类型对象创建的次数,若果我们使用全局变量会会破坏数据的封装,一般的用户代码都可以修改这个全局变量,这时我们可以使用类的静态成员来解决这个问题。

非静态数据成员存在于类类型的每个对象中,static数据成员不属于对象,属于这个类。

staticc成员的优点:

static成员的名字是在类的作用域中,因此可以避开与其他类成员或全局变量的名字冲突。

可以实施封装,static成员可以是私有的,而全局对象不可以。

增强代码的可读性。

总结:

在c++中,static关键字可以修饰类的成员和方法、和对象。

static修饰一个成员,这个成员只能在类外初始化,static修饰的成员属于类而不属于这个类的对象。

static修饰一个成员函数,称为静态成员函数,静态成员函数只能访问静态成员,不能访问非静态成员,但非静态成员函数可以访问静态成员。静态成员函数是没有this指针的。

2、在c++中定义常量的方法

(1)枚举enum

(2)在类内实现 static const int num = 10;





3.const关键字的总结

(1)const修饰变量,称为常量

(2)const修饰方法,只能访问数据成员的值,不能修改成员。

(3)const修饰对象,只能调用const方法,因为const对象不能修改成员变量,而非const变量会试图修改成员变量。

而mutable修饰的成员,即使在const方法、对象,也能修改。

(4)const修饰返回值,只能返回const修饰的变量。

(5)const修饰成员变量时,要在初始化列表初始化。

扩展:

类、对象大小的计算

类的大小计算遵循结构体内存对齐方式。

类的大小与数据成员有关,与成员函数无关。与静态数据成员的大小无关。

类的对象作用域与生命周期:



友元机制

友元是一种允许非类成员函数访问类的非公有成员的一种机制,友元的作用在于提高程序的运行效率,但破坏类的封装性。

(1)友元类与友元函数

友元函数在类作用域外定义,但它需要在类体中进行说明。

定义的个是:friend 类型 友元函数名(参数表);

友元函数使用注意点:

(1)友元函数不是类对的成员函数。友元函数函数可以访问类中的所有成员,一般函数只能访问公有成员。

(2)友元函数不受类中的访问权限的关键字限制,可以放在任意关键字后面,但结果一样。

(3)某类的友元函数的作用域并非该类的作用域。

(4)友元函数破坏了封装性,应尽可能少用。

友元类 friend class string2;

友元类的注意事项:

1、友元关系是单向的 2、友元关系不可传递 3、友元关系不能被继承。

五、运算符重载

运算符重载:编译器自带的运算符,通常不支持自定义类型。目的是提高代码的可读性,体现c++的扩充性。注意:

不要滥用重载、因为它只是语法上的方便,所以只有在涉及的代码更容易写、尤其是更易读时才有必要重载

运算符重载的实现:(1)成员函数重载 (2)友元函数重载

运算符重载规则:

(1)运算符重载不能发明新的运算符

(2)不能改变运算符操作对象的个数

(3)运算符被重载后,其优先级和结合性不会变。

(4)不能程重载的运算符



(5)成员函数与友元函数的重载的选择

一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数

以下一些双目运算符不能重载为类的友元函数:=、()、[]、->。

类型转换运算符只能以成员函数方式重载

流运算符只能以友元的方式重载

Integer类的设计:前置++/后置++的重载

#ifndef _INTEGER_H_
#define _INTEGER_H_

class Integer
{
public:
Integer();
Integer(int n);
Integer(const Integer & other);
Integer & operator=(const Integer & other );
~Integer();
void Display();

//    Integer & operator++();//成员函数重载

friend Integer & operator++(Integer & n);
//    Integer  operator++(int n);

friend Integer operator++(Integer & n, int x);
private:
int n_;
};

#endif

---------------------------------------
#include "Integer.h"
#include <iostream>
using namespace std;
Integer::Integer()
{
}
Integer::Integer (int n)
{
n_ = n;
}
Integer::~Integer()
{
cout << "destroy integer" << endl;
}
Integer::Integer (const Integer & other)
{
n_ = other.n_;
cout << "copy integer "  << endl;
}
Integer & Integer::operator=(const Integer & other)
{
if(this == &other)
{
return *this;
}
n_ = other.n_;
return *this;
}
void Integer::Display()
{
cout << "n_ = " << n_ << endl;
}
//成员函数重载++n
#if 0
Integer & Integer:: operator++()
{
cout << "++n" << endl;
++n_;
return *this;
}
#endif
Integer & operator++(Integer & n)
{
cout << "friend ++n " << endl;
++n.n_;
return n;
}
#if 0
Integer  Integer:: operator++(int n)
{
cout << "n++ " << endl;
Integer tmp(n_);
n_++;
return tmp;
}
#endif
Integer  operator++(Integer & n,int x)
{
cout << "friend n++" << endl;
Integer tmp(n.n_);
n.n_++;
return tmp;
}


实现string类

#ifndef _STRING_H_
#define _STRING_H_

using namespace std;

class String
{
public:
String();
String(char *ptr);
String(const String & other);
~String();
bool operator!() const;//!
/* = 重载*/
String & operator=(const String & other);
String & operator=(const char *ptr);

/*[]*/
char & operator[](unsigned int index);
const char & operator[](unsigned int index) const;

/* + */
friend String operator+(const String & s1,const String & s2);
/* += */

String & operator+=(const String& other);
//流运算符重载

friend istream & operator>>(istream & input, String & s);
friend ostream & operator<<(ostream & output, String & s);
void Display();

private:
char *str;

};

#endif
#include <iostream>
#include "String.h"
#include <string.h>

using namespace std;

String::String()
{
str = new char('\0');
cout << "string 1" << endl;
}

String::String(char *ptr)
{
int len = strlen(ptr) + 1;
str = new char(len);
memset(str,0,len);
strcpy(str,ptr);

cout << "string 2" << endl;
}
String::String(const String & other)
{
int len = strlen(other.str) + 1;

str = new char(len);
memset(str,0,len);

strcpy(str,other.str);
cout << " string 3" << endl;

}

//!重载

bool String::operator!() const
{
return strlen(str) != 0;
}
//= 重载

String & String::operator=(const String & other)
{
if(this == &other)
{
return *this;
}

int len = strlen(other.str) + 1;
delete [] str;
str = new char[len];
memset(str,0,len);
strcpy(str,other.str);

return *this;
}
String & String::operator=(const char *ptr)
{
int len = strlen(ptr) + 1;
delete [] str;
str = new char[len];
memset(str,0,len);
strcpy(str,ptr);

return *this;
}
/*[]*/

char & String::operator[](unsigned int index)
{
cout << "char & operator[]" <<endl;

return str[index];

//    return const_cast<char &>(static_cast<String &>(*this)[index]);

}

const char & String::operator[](unsigned int index) const
{
return str[index];

}
String::~String()
{
cout << "destory string " << str << endl;
delete str;
}

/* + */

String operator+(const String &s1,const String & s2)
{
int len = strlen(s1.str) + strlen(s2.str) + 1;
char *newstr = new char[len];
memset(newstr,0,len);
strcpy(newstr,s1.str);
strcat(newstr,s2.str);

String tmp(newstr);
delete newstr;
return tmp;
}

/* += */

String & String::operator+=(const String &other)
{
int len = strlen(str) + strlen(other.str) + 1;
char *newstr = new char[len];
memset(newstr,0,len);
strcpy(newstr,str);
strcat(newstr,other.str);

delete [] str;
str= newstr;
return *this;
}

//>>

istream & operator>>(istream & input,String & s)
{
char *buffer = new char[1024];
input >> buffer;
int len = strlen(buffer) + 1;
delete [] s.str;
s.str = new char [len];
strcpy(s.str,buffer);
delete [] buffer;

return input;
}

//<<

ostream & operator<<(ostream & output,String & s)
{
output << s.str;
return output;

}
void String:: Display()
{
cout << "str = " << str << endl;
}


问题:流运算符为什么要用友元函数进行重载?

如果是重载双目操作符(即为类的成员函数),就只要设置一个参数作为右侧运算量,而左侧运算量就是对象本身而 >> 或<< 左侧运算量是 cin或cout 而不是对象本身,所以不满足后面一点,就只能申明为友元函数了

类型转化符重载:

1、必须是成员函数,不能是友元函数,没有参数,不能指定返回类型。

2、函数原型:operator 类型名();

代码示例

Integer:: operaotr int ( )
{
return n_;
}

int main()
{
Integer n(1000);
n = 200;
n.Display();
int sum = add(n,100);
cout << sum << endl;
int x = n;
int y = static_cast<int >(n);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  对象 编程 class