c++掌握不充分知识

 本学期学习了三门语言,分别是c++,python和java,为防止知识点混乱(实在是记不住这么多,特别是c++这个万恶之源)和期末考试挂科,特整理自己掌握不好的知识点,仅期末考试复习使用。

目录

头文件

ASCii码表

const

 引用与拷贝构造函数

 拷贝构造函数

return this

char[]和string和char*转换

运算符重载

针对<<>> 的重载

针对+-重载 

针对++,--的重载

 继承

static

 virtual

虚继承

虚析构函数

纯虚函数

继承典型例题

固定函数

引用

while循环


头文件

首先,最重要的是,万能头文件,不要忘记

#include<bits/stdc++.h>

 或者用

#include<iostream>

ASCii码表

大写字母:65-90

小写字母:97-122

const

对于const类对象/指针/引用,只能调用类的const成员函数.

因此,如果在主函数中看到了如下

const Circle c1(2);

则需要看一下他都调用了什么方法,然后在根据方法去加const,位置如下

 double getArea()const
    {

        return 3.14*radius*radius;
    }

但是这种函数局限,即

它不改变对象的成员变量,也不能调用类中任何非const成员函数。

 引用与拷贝构造函数

先看一段代码

    Point& getLeftTop()
    {
        return leftTop;
    }
    Point getRightBottome()
    {
        return rightBottom;
    }

上面返回的是Point&,不会掉用Point的拷贝构造函数,而下面这个函数调用的是Point,所以会在函数掉用Point的拷贝构造函数,这两个的区别比较大,需要稍微理解一下(错了好长时间)

 拷贝构造函数

Point(Point& a)
    {

       //是对的,对象是自身的时候需要用到引用
    }
Point(Point a)
    {

       //错
    }
Circle(Point a):center(a)
    {
        //对,会掉用point的拷贝构造函数
    }
Circle(Point& a):center(a)
    {
        //对,不会调用point的拷贝构造函数
    }

return this

注意别掉了*就行

Computer& setName(char* a)
    {
        name=a;
        return *this;
    }

char[]和string和char*转换

 参考博客:https://blog.csdn.net/amin_hui/article/details/115359358

主要是char[]转char*的时候,需要用到strcpy,其他的都比较正常

char *s="abcdef";
char ch[100];
strcpy(ch,s);

运算符重载

参考博客:https://blog.csdn.net/qq_42683011/article/details/102087764

绝大多数运算符可以通过成员或者非成员函数进行重载,但是下面的运算符只能通过成员函数进行重载                

=:赋值运算符

():函数调用运算符

[]:下标运算符

->:通过指针访问类成员的运算符

针对<<>> 的重载

在类里面先声明:

friend istream& operator>>(istream& in,newInt & a);

在类外进行函数内容填充

istream& operator>>(istream& in,newInt & a)
{
    in>>a.i;
    return in;
}

注意一点就是,类内加friend,带引用

针对+-重载 

//类内
friend newInt& operator+(newInt& a,newInt & b);
//类外
newInt& operator+(newInt& a,newInt & b)
{
}

不多说了,记者引用就行,也可以再加一个const

针对++,--的重载

前置:

	student& operator++(void){		//前置++重载
		++this->num;
		return *this;
	}
	Time operator++( int )          //后置++重载   
      {
         // 保存原始值
         Time T(hours, minutes);(*this)
         // 对象加 1
         ++minutes;                    
         if(minutes >= 60)
         {
            ++hours;
            minutes -= 60;
         }
         // 返回旧的原始值
         return T; 
      }
Time& operator --(void)//前置
    {
        if(h>=24||m>=60||s>=60)
    {
       return *this;
    }
        int ans=(h*3600+m*60+s-1+3600*24)%(3600*24);
        h=ans/3600;
        s=ans%60;
        m=(ans-h*3600)/60;
        return *this;
    }
    Time operator --(int )//后
    {
        if(h>=24||m>=60||s>=60)
    {
        return *this;
    }
        int ans=(h*3600+m*60+s-1+3600*24)%(3600*24);
        Time a=(*this);
        h=ans/3600;
        s=ans%60;
        m=(ans-h*3600)/60;
        return a;
    }

 继承

没啥好说的,上代码

class Dog:public Animal
{
}

static

static 的知识点还真不少

首先,如果在class中定义了static常量,则我们需要再class外给他赋值

class Shape{
static int num;
}
int Shape::num=0;

赋值语句前面别忘了那个int

第二点,如果在main函数中看到通过类调用方法,则需要将方法定义为static

看到这个,则需要

Shape::getNumOfShapes()
class Shape
{
public:
   static int getNumOfShapes()
   {
       return num;
   }
}

这样设计class里面的代码

 virtual

虚继承

 虚继承是为了防止继承多个对象之后,继承来的同名的数据成员在内存中有不同的拷贝造成数据不一致问题。

因此如果继承多个对象有命名重复的对象,则需要使用虚继承,一下为虚继承的实现代码

class Grapefruit:virtual public Orange,virtual public Pomelo
{
    public:
    Grapefruit(string a="",double b=0):Orange(a,b),Pomelo(a,b),Citrus(a,b),Mandarin(a,b)
    {
    }
    void show()
    {
        cout<<name<<" "<<weight<<"kg, is grapefruit."<<endl;
    }

};

虚析构函数

 这个主要是为了保证释放派生对象的时候能够以一个正确的顺序调用析构函数。

virtual ~Teacher()
    {
        cout<<"Teacher's deconstructor."<<endl;
    }

纯虚函数

class Animal
{
public:
   virtual void eat()=0;
};

这样animal这个类就变成了一个抽象类,在继承时需要在子类中去实现里面的虚函数,注意 :纯虚函数需要在后面=0(错了好久)。

继承典型例题

例1:

Description

         Jackie开了一家水果店,店里开展了新业务:榨果汁和水果沙拉。他发现桃子(Peach)不论是榨果汁还是水果沙拉都很受欢迎。除了桃子(Peach)还有3种水果苹果(Apple)、菠萝(Pineapple)、香蕉(Banana)也进入了新业务。

这4种水果(Fruit)被分成了三个种类:榨汁(Juice)类、沙拉(Salad)类、能榨汁也能做沙拉(JuiceSalad)类。其中,菠萝只能榨汁、香蕉只能做沙拉、桃子和苹果是既能榨汁也能做沙拉。

所有的水果都有属性重量(weight)。

能榨汁的水果具有属性榨汁率(juice_ratio)。

能做沙拉的水果也有个属性是做成沙拉后保存率(salad_ratio)。

既能榨汁又能做沙拉的水果,会把60%用来榨果汁、40%用来做沙拉,因此具有juice_ratio和salad_ratio两个属性。

现在有水果的进货数据,请你编写程序计算水果总量、榨汁总量和沙拉总量。

用C++编写Peach类、Apple类、Banana类、Pineapple类和它们的继承关系来完成代码,调用格式见“Append Code”。

Fruit::weight()水果的重量。

juice_weight()和salad_weight()分别用来计算水果榨汁的重量和做成沙拉的重量,都是多态函数。

各类的构造根据给出的调用代码完成。

Input

         第一行输入整数k,表示后面有k个记录。每个记录水果的名称、重量、榨汁率和做成沙拉后的保存率,只能榨汁或只能做沙拉的水果仅有一个能用的比例。

Output

         输出这些水果的总重量、榨汁的总量和做成沙拉的总量。

Sample Input

4 Peach 100 0.67 0.55 Pineapple 50 0.5 Banana 80 0.88 Apple 200 0.4 0.6

Sample Output

All fruits weight 430 kg.

These fruits can juicing 113.2 kg fruit juice.

These fruits can making 140.4 kg fruit salad.

代码

#include<iostream>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
class Fruit
{
public:
    double weight1;

    Fruit(double a=0):weight1(a){}
    double weight()
    {
        return weight1;
    }
};

class Juice:virtual public Fruit
{
public:
    double juice_ratio;
    Juice (double w=0,double a=0):Fruit(w),juice_ratio(a){}
    virtual double juice_weight()=0;

};

class Salad:virtual public Fruit
{
 public:
    double salad_ratio;
    Salad(double w=0,double a=0):Fruit(w),salad_ratio(a){}
    virtual double salad_weight()=0;
};

class JuiceSalad:public Juice, public Salad
{
public:

    JuiceSalad(double w=0,double a=0,double b=0):Fruit(w),Juice(w,a),Salad(w,b){}
    double salad_weight()
    {
        return weight1*salad_ratio*0.4;
    }
    double juice_weight()
    {
        return weight1*juice_ratio*0.6;
    }
};
class Peach:virtual public JuiceSalad
{
public:
    Peach(double a,double b,double c):Fruit(a),JuiceSalad(a,b,c){}

};

class Apple:virtual public JuiceSalad
{
public:
    Apple(double a,double b,double c):Fruit(a),JuiceSalad(a,b,c){}

};

class Pineapple:public Juice
{
public:
     Pineapple(double a,double b):Fruit(a),Juice(a,b){}
     double juice_weight() {
        return weight1 * juice_ratio;
    }
};

class Banana :virtual public Salad
{
public:
    Banana(double a,double b):Fruit(a),Salad(a,b){}
    double salad_weight() {
        return weight1 * salad_ratio;
    }
};

int main()
{
    Fruit* fruit[100];
    Juice* juice[100];
    Salad* salad[100];

    string name;
    int cases, f, j, s;
    f = j = s = 0;
    cin >> cases;
    while(cases--)
    {
        cin >> name;
        double weight, s_ratio, j_ratio;
        if(name == "Peach")
        {
            cin >> weight >> j_ratio >> s_ratio;
            Peach* p = new Peach(weight, j_ratio, s_ratio);
            fruit[f++] = p;
            juice[j++] = p;
            salad[s++] = p;
        }
        if(name == "Pineapple")
        {
            cin >> weight >> j_ratio;
            Pineapple* p = new Pineapple(weight, j_ratio);
            fruit[f++] = p;
            juice[j++] = p;
        }
        if(name == "Banana")
        {
            cin >> weight >> s_ratio;
            Banana* b = new Banana(weight, s_ratio);
            fruit[f++] = b;
            salad[s++] = b;
        }
        if(name == "Apple")
        {
            cin >> weight >> j_ratio >> s_ratio;
            Apple* a = new Apple(weight, j_ratio, s_ratio);
            fruit[f++] = a;
            juice[j++] = a;
            salad[s++] = a;
        }
    }

    double fruit_weight, salad_weight, juice_weight;
    fruit_weight = salad_weight = juice_weight = 0;
    for(int i = 0; i < f; i++)
        fruit_weight += fruit[i]->weight();
    cout << "All fruits weight " << fruit_weight << " kg." << endl;
    for(int i = 0; i < j; i++)
        juice_weight += juice[i]->juice_weight();
    cout << "These fruits can juicing " << juice_weight << " kg fruit juice." << endl;
    for(int i = 0; i < s; i++)
        salad_weight += salad[i]->salad_weight();
    cout << "These fruits can making " << salad_weight << " kg fruit salad." << endl;
}

注意几点:

1.

class Juice:virtual public Fruit
{
public:
    double juice_ratio;
    Juice (double w=0,double a=0):Fruit(w),juice_ratio(a){}
    virtual double juice_weight()=0;

};

这个位置的函数设置为virtual,是因为后面那个可以榨汁可以做沙拉的类也有一个同样的函数,因此我们需要将它设置成virtual,然后再在继承他的其他类里面实现它。

2.juicy和salad里面都需要有重量,不然下面的函数没法实现。

3.如果一个类的方法被继承时想要被重载,就必须要变成virtual类型(虚函数)

例2

还有一个比较长的例题,具体就是,一个类继承了父类,父类又继承了自己的父类,这样在构造的时候都要附上值,不然输出会有问题。

例3

Description

定义Integer类,只有一个int类型的属性value。包括如下成员函数:

1. void setValue(int): 设置value为参数的值。

2. int getValue():获取value的值。

3. 重载乘法运算。新的乘法定义为:对于数值n,如果乘数是m,那么将n重复m次形成一个新的数。如:34 * 3 = 343434。注意:34 * 1 = 34。

4. 重载=运算符。

Input

输入有多行,第1行是Integer类的对象M的属性值。

之后的第2行N表示后面还有N行输入,每行输入是一个正整数,表示对M的乘数。

Output

N个新的Integer对象的值,每个是M的值乘以相应的乘数。假定所有的输出都不会溢出。

Sample Input

1 10 1 2 3 4 5 6 7 8 9 10

Sample Output

1

11

111

1111

11111

111111

1111111

11111111

111111111

1111111111

代码:

#include<iostream>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
class Integer
{
public:
    int value;
     void setValue(int a)
     {
         value=a;
     }
     int getValue()
     {
         return value;
     }
     Integer operator*(int a)
     {
         int n=0;
         int temp=value;
         while(temp>0)
         {
             n++;
             temp=temp/10;
         }
         int ans=0;
         int n1=n;

         for(int i=0;i<a;i++)
         {

             ans=ans*pow(10,n1)+value;
         }
         Integer temm;
         temm.value=ans;
         return temm;
     }
     Integer& operator =(const Integer &a)
     {
         this->value=a.value;
         return *this;
     }

};
int main()
{
    Integer M, N;
    int a, n, m;
    cin>>a;
    M.setValue(a);
    cin>>n;
    while (n--)
    {
        cin>>m;
        N = M * m;
        cout<<N.getValue()<<endl;
    }
    return 0;
}

固定函数

round():四舍五入

pow():次方

sort():

    vector<int> m;
    m.push_back(a);
    m.push_back(b);
    m.push_back(c);
    sort(m.begin(),m.end());

sort的使用方法可一定要记着啊!!!

引用

Point &setPoint(int a,int  b)
    {
        this->a=a;
        this->b=b;
        return *this;
    }

前面为Point&,则返回引用,不会在重新创建一个对象,如果返回值为Point类型,则需要再重新创建一个对象。

while循环

while(cin>>s)
{
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值