Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.0 kB
2
Indexable
Never
//mammal.h
#ifndef _MAMMAL_H
#define _MAMMAL_H

#include <string>
#include <iostream>
using std::ostream;
using std::string;

class Mammal
{
public:
	Mammal();
	Mammal(string species);
	~Mammal();
	virtual Mammal& operator = (const Mammal& rhs);		//copy assignment
	Mammal(const Mammal& other);						//copy constructor
	Mammal& operator = (Mammal&& rhs) noexcept;			//move assignment 
	Mammal(Mammal&& other) noexcept;					//move constructor
	void WhatAmI();
	const string GetSpecies() const;

private:
	string m_species;
};



//cpp file
#include <iostream>
#include "Mammal.h"

using std::cout;
using std::endl;

Mammal::Mammal() : m_species("")
{
	cout << "Mammal default ctor" << endl;
}

Mammal::Mammal(string species) : m_species(species)
{
	cout << "Mammal one arg ctor" << endl;
}

Mammal::~Mammal()
{
	cout << "Mammal dtor" << endl;
}

Mammal& Mammal::operator = (const Mammal& rhs)
{
	cout << "Mammal copy =" << endl;
	if (this != &rhs)
	{
		m_species = rhs.m_species;
	}
	return *this;
}

Mammal::Mammal(const Mammal& other) : m_species(other.m_species)
{
	cout << "Mammal copy ctor" << endl;
}

Mammal& Mammal::operator = (Mammal&& rhs) noexcept
{
	cout << "Mammal move =" << endl;
	if (this != &rhs)
	{
		m_species = static_cast<string&&> (rhs.m_species);
	}
	return *this;
}

Mammal::Mammal(Mammal&& other) noexcept : m_species(other.m_species)
{
	cout << "Mammal move ctor" << endl;
}

void Mammal::WhatAmI()
{
	cout << "I am a mammal" << endl;
}

const string Mammal::GetSpecies() const
{
	return m_species;
}

#pragma once
#ifndef _ELK_H
#define _ELK_H

#include "Mammal.h"

class Elk : public Mammal
{
public:
	Elk();
	Elk(int points);
	~Elk();
	virtual Elk& operator = (const Elk& rhs);
	Elk(const Elk& copy);
	Elk& operator = (Elk&& rhs) noexcept;
	Elk(Elk&& copy) noexcept;
	void WhatAmI();

protected:
	int m_points;
};
#endif




//elk.cpp
#include <iostream>
#include "Elk.h"

using std::cout;
using std::endl;

Elk::Elk() : Mammal("Elk"), m_points(0)
{
	cout << "Elk default ctor" << endl;
}

Elk::Elk(int points) : Mammal("Elk"), m_points(points)
{
	cout << "Elk one arg ctor" << endl;
}

Elk::~Elk()
{
	cout << "Elk dtor" << endl;
}

Elk& Elk::operator = (const Elk& rhs)
{
	cout << "Elk copy =" << endl;
	if (this != &rhs)
	{
		Mammal::operator = (rhs);
		m_points = rhs.m_points;
	}
	return *this;
}

Elk::Elk(const Elk& other) : Mammal(other), m_points(other.m_points)
{
	cout << "Elk copy ctor" << endl;
}

Elk& Elk::operator = (Elk&& rhs) noexcept
{
	cout << "Elk move =" << endl;
	if (this != &rhs)
	{
		Mammal::operator = (static_cast<Elk&&>(rhs));
		m_points = rhs.m_points;
	}
	return *this;
}

Elk::Elk(Elk&& other) noexcept : Mammal(static_cast<Elk&&>(other)), m_points(other.m_points)
{
	cout << "Elk move ctor" << endl;
}

void Elk::WhatAmI()
{
	cout << "I am an elk" << endl;
}