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>

#define HI std::cout << __PRETTY_FUNCTION__ << '\n';		// A macro—how quaint.

class Loud {
  public:
    Loud() { HI; }						// default ctor
    Loud(int i) { std::cout << "Loud::Loud(" << i << ")\n"; }
    Loud(char c) { std::cout << "Loud::Loud('" << c << "')\n"; }
    ~Loud() { HI; }						// dtor
    Loud(const Loud &) { HI; }					// copy ctor
    Loud(const Loud &&) { HI; }					// move ctor
    Loud &operator=(const Loud &) { HI; return *this; }		// assignment
    Loud &operator=(const Loud &&) { HI; return *this; }	// move
    Loud &operator=(int) { HI; return *this; }
    Loud &operator=(char) { HI; return *this; }
    Loud &operator++() { HI; return *this; }			// preincrement
    Loud &operator++(int) { HI; return *this; }			// postincrement
    Loud operator+(const Loud &) const { HI; return Loud(); }	// +
};

#undef HI

#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