Sitat av
Xasma
Ingen kommer til å beskrive hele koden og alle kall i detalj for deg, det er for omfattende, og mest sannsynlig i stor grad pløye pløyd mark, da vi må anta du gjør dette med eksamen i tankene og kan noe - men vi vet ikke hva. Forklar hva du tror at skjer, linje for linje, kall for kall, og legg litt innsats i det, så skal du ikke se bort ifra at noen hjelper deg.
Men om du ikke gidder mer enn å copy paste kode og si "forklar meg", da gidder ingen.
Jeg vet ikke hvordan du ser på denne koden som omfattende å forklare, den er rimelig enkel. Men samtidig så er ikke koden spesielt pen, og den følger heller ikke stilen til C++, eller retningslinjene. I tillegg så er det endel effektiviserings potensiale. Man kan jo undre seg over hvordan forfatteren har klart å gjøre en så dårlig jobb med 27 linjers lang kode. Jeg ville ha skrevet den slik:
Kode
// header.h
#include <cstdlib>
#include <iostream>
#include <string>
class Object
{
private:
std::string txt;
char tegn;
int tall;
public:
Object(std::string, char, int);
void funk1() const;
bool funk2(const char, const int, const int) const;
char funk3() const;
void funk4(int);
};
Kode
// source.cpp
#include "header.h"
Object::Object(std::string tx, char te, int ta): txt(tx), tegn(te), tall(ta) {}
void Object::funk1() const
{
std::cout << this -> txt << ' ' << this -> tegn << ' ' << this -> tall << std::endl;
}
bool Object::funk2(const char te, const int n1, const int n2) const
{
return(txt[n1] == te || txt[n2] == te);
}
char Object::funk3() const
{
return(static_cast<char>(static_cast<int>(this -> tegn) + this -> tall));
}
void Object::funk4(int ta)
{
this -> tall = ta;
}
int main(int argc, char **argv)
{
Object ob1("Arsenal", 'A', 5);
Object ob2("Chelsea", 'H', 3);
std::cout << ob2.funk2('e', 3, 6) << std::endl;
std::cout << ob1.funk2('a', 0, 5) << ' ' << ob2.funk2('E', 2, 5) << std::endl;
ob2.funk4(-7);
std::cout << ob1.funk2(ob2.funk3(), 0, 1) << std::endl;
return(EXIT_SUCCESS);
}
Eller bedre, bruke getters og setters. Jeg har skrevet getters og setters som functors i koden under.
Kode
// header.h
#include <cstdlib>
#include <iostream>
#include <string>
template <typename T> class Attribute
{
private:
T attribute;
public:
Attribute();
Attribute(T);
T operator()() const;
void operator()(T);
};
class Object
{
private:
Attribute<std::string> txt;
Attribute<char> tegn;
Attribute<int> tall;
public:
Object(std::string, char, int);
void funk1() const;
bool funk2(const char, const int, const int) const;
char funk3() const;
void funk4(int);
};
Kode
// source.h
#include "header.h"
template <typename T> Attribute<T>::Attribute() {}
template <typename T> Attribute<T>::Attribute(T attribute)
{
(*this)(attribute);
}
template <typename T> T Attribute<T>::operator()() const
{
return(this -> attribute);
}
template <typename T> void Attribute<T>::operator()(T attribute)
{
this -> attribute = attribute;
}
Object::Object(std::string tx, char te, int ta):
txt(Attribute<std::string>(tx)),
tegn(Attribute<char>(te)),
tall(Attribute<int>(ta))
{}
void Object::funk1() const
{
std::cout << this -> txt() << ' ' << this -> tegn() << ' ' << this -> tall() << std::endl;
}
bool Object::funk2(const char te, const int n1, const int n2) const
{
return(txt()[n1] == te || txt()[n2] == te);
}
char Object::funk3() const
{
return(static_cast<char>(static_cast<int>(this -> tegn()) + this -> tall()));
}
void Object::funk4(int ta)
{
this -> tall(ta);
}
int main(int argc, char **argv)
{
Object ob1("Arsenal", 'A', 5);
Object ob2("Chelsea", 'H', 3);
std::cout << ob2.funk2('e', 3, 6) << std::endl;
std::cout << ob1.funk2('a', 0, 5) << ' ' << ob2.funk2('E', 2, 5) << std::endl;
ob2.funk4(-7);
std::cout << ob1.funk2(ob2.funk3(), 0, 1) << std::endl;
return(EXIT_SUCCESS);
}
Sist endret av 0xFF; 7. desember 2017 kl. 18:23.