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

copy constructor c++ syntax

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

copy constructor in c++ questions

#include<iostream> 
using namespace std; 
class ABC { 
 public:ABC(){
    cout<<"Constructor is called automatically";
    cout<<"at the time of execution"<<endl;
 }
 void display(){
        cout<<"hello world";
 } 
}; 

int main() { 
 ABC ob1,ob2;
 ob1.display();
 ob2.display();
 return 0;  
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ create a vecto 
Cpp :: how to know how many numbers i deleted with erase command on multiset c++ 
Cpp :: ue_log example 
Cpp :: how to test if char in = to another in c++ 
Cpp :: c++ insert vector into vector 
Cpp :: interactive problem 
Cpp :: Stream Overloading 
Cpp :: Studying Alphabet codechef solution in c++ 
Cpp :: ala vida 
Cpp :: sort n characters in descending order c++ 
Cpp :: auto keyword 
Cpp :: set app icon qt 
Cpp :: c++ code 2d block 
Cpp :: c++ operators 
Cpp :: sin trigonometric function 
Cpp :: C++ Point to Every Array Elements 
Cpp :: skip headers while reading text 
Cpp :: rand function c++ 
Cpp :: c++ to c converter 
Cpp :: reference variablesr in c++ 
Cpp :: operator overloading 
Cpp :: print number with leading zeros 
Cpp :: constants in cpp 
Cpp :: split string by delimiter cpp 
Cpp :: declaring multiple variables in cpp 
Cpp :: english to french typing online 
C :: java.lang.SecurityException: Permission denied (missing INTERNET permission?) 
C :: ruby absolute value 
C :: sdl draw Rectf 
C :: How to generate a random array in c 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =