test passing parameters in c++

1

Click here to load reader

Upload: michel-alves

Post on 25-May-2015

65 views

Category:

Education


1 download

DESCRIPTION

Test Passing Parameters in C++.

TRANSCRIPT

Page 1: Test Passing Parameters in C++

#include <iostream>

/*** Test class.*/

class Class{/*** Constructor.*/int _id;

public:/*** Constructor.*/

Class(int id = 0): _id(id){ std::cout << "Constructor Invocation! Id: " << _id << std::endl; };

/*** Id Method.*/

void id(int myid){ _id = myid;}

/*** Run method.*/

void run(void){ std::cout << "Running! Id: " << _id << std::endl; };

/*** Destructor.*/

virtual ~Class(void){ std::cout << "Destructor Invocation! Id: " << _id << std::endl;}

};

// Function with passing parameter by valuevoid TestFunction(Class c){ c.id(1); c.run(); };// Function with explicit parameter passing by reference.void TestFunction(Class* c){ c->run(); };// Function with parameter passing by automatic dereference.void TestFunction2(Class& c) { c.run(); };

int main(void){

Class c(0);

std::cout << "\n@@@" << std::endl;TestFunction(c);std::cout << "@@@" << "\n\n";

std::cout << "---" << std::endl;TestFunction(4);std::cout << "---" << "\n\n";

std::cout << "+++" << std::endl;TestFunction(&c);std::cout << "+++" << "\n\n";

std::cout << "$$$" << std::endl;TestFunction2(c);std::cout << "$$$" << "\n\n";

return 0;}