Basic Copy Constructor and Using it – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#7463    siteicon   siteicon  

Basic Copy Constructor and Using it

Understanding copy constructor concept as a need to copy objects by passing reference of another object of same class.

Learning Objectives

  • Understanding copy constructor for creating new objects of same class based on another object.

Source Code

TC++ #7463


Source Code

Run Output

Using Constructor, x=10
Using Copy Constructor, x=10
Using Copy Constructor, x=10

Code Understanding

class C {  //Declaring the class template with name C
int x;          //A private variable in the class
C(){ x=10, cout<<“Using Constructor, x=”<<x<<endl; }
A default constructor that would set x as 0 and display message and value of x.

C(const C& CC) { x=CC.x; cout<<“Using Copy Constructor, x=”<<x<<endl; }
A copy constructor that would use reference tp parameter of class object CC and would set the value of x based on value of x set by constructor. Will display message and value of x.


int main(){
C c1;            //Declaring c1 object which will call default constructor.
C c2=c1;     //Declaring c2 object which will call copy constructor being derived from object c1
C c3(c1);    //Alternative way of declaring object which will call copy constructor being derived from object c1
return 0;


  • Copy constructor is called when object copy is being made in a specific way of using the reference of another object of same class.
  • The object reference called as parameter to copy constructor is generally defined as const so that the original references of the calling object are not disturbed by the copy constructor.

Suggested Filename(s): cc.cpp,copycons.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