-1

this is main file

#include <bits/stdc++.h>
#include "animal.h"
#include<sstream>

using namespace  std ;

int main(){

    Animal elephant("ele" ,12);
    Animal cow("cow" ,22) ;
    cow = elephant ;
    cow.a[0]=5 ;
    return 0 ;
}

this is Animal.h file

#ifndef ANIMAL_H
#define ANIMAL_H

#include<iostream>
#include<string>
using namespace std ;
class Animal{
    string name ;
    int age  ;

public :
    int a[] ;
    Animal(string name , int age ):name(name) ,age(age) {}
    Animal(const Animal & other);


};

#endif // ANIMAL_H

this is Animal.cpp

#include"animal.h"
#include<iostream>
using namespace std ;
Animal::Animal(const Animal & other){
    cout<<"copy constructor is called"<<endl ;
    this->age=other.age ;
    this->name = other.name ;
}

I am not able to call the copy constructer ?? what is wrong with code . I have given all the files with their names and code .

vishal
  • 855
  • 1
  • 8
  • 16
  • 2
    This `#include ` is not part of Standard C++ - you should not be using it. And you don't seem to understand the difference between copy construction and assignment. –  Aug 04 '18 at 21:12
  • What actually happens? A linker error? – πάντα ῥεῖ Aug 04 '18 at 21:12
  • Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Aug 04 '18 at 21:13
  • Also please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ to learn some reasons your question might be down-voted. Finally, please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Aug 04 '18 at 21:14
  • `int a[] ;` isn't valid. – πάντα ῥεῖ Aug 04 '18 at 21:14
  • And I don't see anywhere the copy-constructor would be invoked. I see a place where the *copy-assignment operator* is invoked though. – Some programmer dude Aug 04 '18 at 21:15
  • You should follow the [rule of three](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)). Meaning that classes which have a copy constructor usually need an assignment operator and a destructor as well. Not implementing one of them usually leads to pain and suffering. (Which you already experienced xD ) You might skip the destructor in certain cases, but not the rest. – jan.sende Aug 04 '18 at 21:45
  • By the way, because your class is very simple, you can let the compiler do the work and use: `Animal(const Animal & other) = default;`, `Animal() = default;`, and `Animal& operator=(const Animal&) = default;` in the class definition. Furthermore, please consider using [`#pragma once`](https://en.wikipedia.org/wiki/Pragma_once) instead of macros, plus don't use [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) if possible. – jan.sende Aug 04 '18 at 21:51

1 Answers1

2

After

Animal cow("cow" ,22) ;

cow exists. It has been constructed. It cannot be constructed again, so

cow = elephant ;

is an assignment and invokes operator=, the assignment operator. Lets add an assignment operator

Animal & Animal::operator=(const Animal & other){
    cout<<"Assignment operator is called"<<endl ;
    this->age=other.age ;
    this->name = other.name ;
}

to Animal and see what happens: https://ideone.com/WlLTUa

Animal cow = elephant

would invoke the copy constructor (Example: https://ideone.com/sBdA1d)

Also read up on Copy Elision for another great trick that can lead to "Dude, Where's my copy?" type questions.

user4581301
  • 33,082
  • 7
  • 33
  • 54