Copy Constructor Call When Passed as Value to A Function – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#7470    siteicon   siteicon  

Copy Constructor Call When Passed as Value to A Function

Understanding copy constructor call when class object is passed as value to a function.

Learning Objectives

  • Viewing how copy constructor is called when object are passed by value to a function.

Source Code

TC++ #7470


Source Code

Run Output


Code Understanding

int x,y;  //Global variables declared so that this demo allows them to use from different functions.
class C { //Class C declared
C(){ x=10;}  //Default Constructor sets x as 10.
C(C& D) { x=20;} //Copy Constructor sets x as 20.

void func(C E){ } //Function where object E of type C is passed as value.  Function doesn’t perform any job. It will just depict use of copy constructor as object has been passed as value here.

int main(){
C c1;    //c1 is an object based on C, so just default constructor would be called.
cout<<x<<endl; //This prints 10 as default constructor has set x as 10.
func(c1);  //Actual function call with object of C which is c1. Now copy constructor would be called.
cout<<x<<endl; //This would print 20 as copy constructor has now been called.
return 0;


  • Copying copy constructor can be called in many other situations like
    – Creating copy of an object.
    – When Object is called by value to a function.
    – When Object is returned from a function.
    – When temporary objects are created.

Common Errors

  • If function calls object by reference like this void func(C& E){ } then only normal constructor would run and value 10 10 would be printed.

Suggested Filename(s): objpassedasvalue.cpp,objpval.cpp


CSKC| Created: 26-Apr-2019 | Updated: 26-Apr-2019|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues