fork(1) download
  1. // mushing everyting into one file for easier demonstration
  2. #include<iostream>
  3. #include<string>
  4. using namespace std ; // this is a dangerous thing to place in a header
  5. // because it is applied to everything that includes this header
  6. // you can break files that count on proper scope resolution
  7. class Animal{
  8. string name ;
  9. int age ;
  10.  
  11. public :
  12. int a[] ;
  13. Animal(string name , int age ):name(name) ,age(age) {}
  14. Animal(const Animal & other);
  15. Animal& operator=(const Animal & other);
  16. };
  17.  
  18. //#include <bits/stdc++.h> don't use stuff from bits. The are internal implementation headers
  19. #include<sstream>
  20.  
  21. int main(){
  22.  
  23. Animal elephant("ele" ,12);
  24. Animal cow("cow" ,22) ;
  25. cow = elephant ;
  26. cow.a[0]=5 ;
  27. return 0 ;
  28. }
  29.  
  30. Animal::Animal(const Animal & other){
  31. cout<<"copy constructor is called"<<endl ;
  32. this->age=other.age ;
  33. this->name = other.name ;
  34. }
  35. Animal & Animal::operator=(const Animal & other){
  36. cout<<"Assignment operator is called"<<endl ;
  37. this->age=other.age ;
  38. this->name = other.name ;
  39. }
Success #stdin #stdout 0s 4328KB
stdin
Standard input is empty
stdout
Assignment operator is called