Developing a program in C++ : Constructor overloading

in #program6 years ago

My name is plawasha and i,m student of software engineering. but as i,m new in development i am learning about development how to develop a program and it is very tough to learn development in this age as i,m 21 year old so i,m working hard to learn about development and basically i,m student of economic and i start learning development due to my fiance and he is working with me to teach me how to develop a program.

I develop a program in C ++ and it has alot of error which compiler is not showing as the errors they are logical errors.

p3.PNG

#include< iostream>
using namespace std;
class mota{
private:
int a,b;
public:
mota(){
int a=0;
int b=0;
}
mota(int x){
a=b=x;
}
mota(int x, int y)
{
a=x;
b=y;
}
int display(){
cout<<"enter value of a"<<a<<endl;
cout<<"enter value of b"<<b<<endl;
}
};
int main (){
mota a,b(100),x(200);
cout<<"constructor 1 by object 1\n";
a.display();
cout<<"constructor 2 by object 2\n";
b.display();
cout<<"constructor 3 by object 3 \n";
x(6).display();
return 0;
}

this is a program of overloading of a constructor.

What is constructor ??

A constructor is a member function of a class which initializes objects of a class. In C++,Constructor is automatically called when object(instance of class) create.It is special member function of the class.

What is constructor overloading ??

In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments.This concept is known as Constructor Overloading and is quite similar to function overloading.

Overloaded constructors essentially have the same name (name of the class) and different number of arguments.
A constructor is called depending upon the number and type of arguments passed.

While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.

simply constructor overloading is the phenomena of making more then one constructor with different parameters.

What are parameters ??

Parameters and arguments. ... The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input supplied at function call.

After all of the mistakes and problems i develop a new and simple program in C++ with the help of constructor overloading phenomena and it works properly.

p1.PNG

#include < iostream>
using namespace std;

class sum{
private:
int a , b ;
public :

    sum (int c){
        c = 20;
        cout<<"value of c " <<c<<endl;
    }
    
    sum(int x , int y){
        cout<<"constructor body\n\n";
        a = x ;
        b = y ;
        
        cout<<"value of a = "<<a<<endl;
        cout<<"value of b = "<<b<<endl;
    }

};

int main ()
{
sum a(10);
sum a1(100 , 200);

}

Output of the program
p.PNG

The Reason of making this post is the tell you nothing is simple everyone has it,s own skill but working hard is the key of the success. work work work hard and success will be yours.

Join steemit school where you can get knowledge about life and steemit how you can make money everything. If you want to be successful then join this channel and if you don,t want to be successful then this is not for you.

https://discord.gg/qKQaeZJ

Sort:  

This is a great post over here

I am a student of bioinformatics. I am a programmer

Overloading was little hard for me in start

Nice post keep it up. i can teach you more about the development :P

Can you teach me this too?