CS253: Software Development with C++

Spring 2018

Init Vs Assign

See this page as a slide show

CS253 Init Vs Assign

Loud.h

% cat ~cs253/Examples/Loud.h
// A “Loud” class.  It announces whenever its methods are called.
#ifndef LOUD_H_INCLUDED
#define LOUD_H_INCLUDED

#include <iostream>

class Loud {
    char c;
    void hi(const char *s) const { 
	std::cout << "Loud::" << s;
	if (c) std::cout << " [c='" << c << "']";
	std::cout << '\n';
    }
  public:
    Loud(char ch = 0) : c(ch) { hi("Loud()"); }
    ~Loud() { hi("~Loud()"); }
    Loud(const Loud &l) : c(l.c) { hi("Loud(const Loud &)"); }
    Loud(const Loud &&l) : c(l.c) { hi("Loud(const Loud &&)"); }
    Loud &operator=(const Loud &l) { c=l.c; hi("operator=(const Loud &)"); return *this; }
    Loud &operator=(const Loud &&l) { c=l.c; hi("operator=(const Loud &&)"); return *this; }
    Loud &operator=(char ch) { c = ch; hi("operator=(char)"); return *this; }
    Loud &operator++() { ++c; hi("operator++()"); return *this; }
    Loud &operator++(int) { c++; hi("operator++(int)"); return *this; }
    Loud operator+(const Loud &l ) const { hi("operator+(const Loud &)"); return Loud(c+l.c); }
};

#endif /* LOUD_H_INCLUDED */

Example

#include "Loud.h"

int main() {
    Loud a(12);
    Loud b(a);
    Loud c=a;
    Loud d();
    c = ++b;
}
Loud::Loud(12)
Loud::Loud(const Loud&)
Loud::Loud(const Loud&)
Loud& Loud::operator++()
Loud& Loud::operator=(const Loud&)
Loud::~Loud()
Loud::~Loud()
Loud::~Loud()

Questions & Answers

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:54

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building