Search
 
SCRIPT & CODE EXAMPLE
 

CPP

How to make copy constructor in c++

#include <iostream>
using namespace std;

// declare a class
class Wall {
  private:
    double length;
    double height;

  public:

    // initialize variables with parameterized constructor
    Wall(double len, double hgt) {
      length = len;
      height = hgt;
    }

    // copy constructor with a Wall object as parameter
    // copies data of the obj parameter
    Wall(Wall &obj) {
      length = obj.length;
      height = obj.height;
    }

    double calculateArea() {
      return length * height;
    }
};

int main() {
  // create an object of Wall class
  Wall wall1(10.5, 8.6);

  // copy contents of wall1 to wall2
  Wall wall2 = wall1;

  // print areas of wall1 and wall2
  cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
  cout << "Area of Wall 2: " << wall2.calculateArea();

  return 0;
}
Comment

create copy constructor c++

// Copy constructor 
    Point(const Point &p2) {x = p2.x; y = p2.y; } 
  
    int getX()            {  return x; } 
    int getY()            {  return y; } 
}; 
Comment

copy constructor c++ syntax

Point(const Point &p2) {x = p2.x; y = p2.y; } 
Comment

C++ copy constructor

// Example: Explicit copy constructor
  
#include<iostream>
using namespace std;
  
class Sample
{
    int id;
    public:
    void init(int x)
    {
        id=x;    
    }    
    Sample(){}  //default constructor with empty body
      
    Sample(Sample &t)   //copy constructor
    {
        id=t.id;
    }
    void display()
    {
        cout<<endl<<"ID="<<id;
    }
};
int main()
{
    Sample obj1;
    obj1.init(10);
    obj1.display();
      
    Sample obj2(obj1); //or obj2=obj1;    copy constructor called
    obj2.display();
    return 0;
}
Comment

C++ Copy Constructor

// Example: Implicit copy constructor
  
#include<iostream>
using namespace std;
  
class Sample
{          
      int id;
    public:
    void init(int x)
    {
        id=x;    
    }    
    void display()
    {
        cout<<endl<<"ID="<<id;
    }
};
  
int main()
{
    Sample obj1;
    obj1.init(10);
    obj1.display();
      
    Sample obj2(obj1); //or obj2=obj1; 
    obj2.display();
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: ex: cpp 
Cpp :: create vectors of vectors c++ 
Cpp :: c++ check first character of string 
Cpp :: ? in cpp 
Cpp :: enum in c++ 
Cpp :: cpp language explained 
Cpp :: memset c++ 
Cpp :: use declaration to define a variable 
Cpp :: C++ insert character 
Cpp :: bitmap 
Cpp :: substring function in c++ 
Cpp :: c++ square and multiply algorithm 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: flipperRSocketResponder.cpp command failed react native 
Cpp :: wgat is duble in c++ 
Cpp :: how to run cpp in visual studio 
Cpp :: uint16_t does not name a type 
Cpp :: c create 1 bit value 
Cpp :: c++ vector move element 
Cpp :: C++ float and double simple example 
Cpp :: find substring after character 
Cpp :: function param pointer to struct prototype in c 
Cpp :: passing reference to thread c++ 
Cpp :: Create an algorithm to identify what is the next largest element on a stack (using stack/queue operations only) INPUT: [ 10, 3, 1, 14, 15, 5 ] OUTPUT: 10 - 14 3 - 14 1 - 14 14 - 15 15 - -1 5 - -1 
Cpp :: last index of array c++ 
Cpp :: convert char to string c++ 
Cpp :: how to calculate 2^7 in cpp code 
Cpp :: stack using cpp 
Cpp :: crtdbg c++ 
Cpp :: online compiler c++ with big O calculatorhttps://www.codegrepper.com/code-examples/cpp/how+to+convert+string+to+wchar_t+in+c%2B%2B 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =