Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Function Overloading

With function overloading, multiple functions can have the same name with
different parameters:

How does Function Overloading work?


 Exact match:- (Function name and Parameter)
 If  a not exact match is found:–
               ->Char, Unsigned char, and short are promoted to an int.
               ->Float is promoted to double
 If no match is found:
               ->C++ tries to find a match through the standard conversion.

 ELSE ERROR 
A copy constructor is a member function that initializes an object using
another object of the same class. In simple terms, a constructor which
creates an object by initializing it with an object of the same class, which has
been created previously is known as a copy constructor.  
Copy constructor is used to initialize the members of a newly created object
by copying the members of an already existing object.
Shallow Copy:
In shallow copy, an object is created by simply copying the data of all
variables of the original object. This works well if none of the variables of the
object are defined in the heap section of memory . If some variables are
dynamically allocated memory from heap section, then the copied object
variable will also reference the same memory location.
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class Hero{
int health;
public:
char *name;
char level;
Hero(){
cout<<"Default constructor called\n";
name = new char[100];
}
//parametrized constructor
Hero(int health){ //OUTPUT
this -> health = health; //Helath is 40
//level is g
}
Hero(int health,char level){
this->health = health;
this->level = level;
}
//user defined copy constructor
// Hero(Hero &temp){
// this->health = temp.health;
// this->level = temp.level;
// this ->name = temp.name;
// }
void print(){
cout<<"Helath: "<<this->health<<endl;
cout<<"level: "<<this->level<<"\n";
cout<<"Name: "<<this->name<<"\n";

}
void sethealth(int health){
this->health = health;
}
void setlevel(int level){
this ->level = level;
}
void setname(char name[]){
strcpy(this->name,name);
}
};
int main()
{
Hero hero1;

hero1.sethealth(34);
hero1.setlevel('K');
char name[7] = "RITESH";
hero1.setname(name);
hero1.print();
cout<<"\n";
//use default copy constructor and create a object
Hero hero2 = hero1;
hero2.print();
cout<<"\n";
hero1.name[0] = 'M';
hero1.print();
cout<<"\n";
hero2.print();

return 0;
}
Static member function can only access static member
PRIVATE DATA MEMBER OF ANY CLASS CAN NOT BE INHERITED

You might also like