Skip to main content

How to start learning Web-development

Class and Object , function overloading Explanation in c++ language

 Classes in C language -

It is a user defined data type data type which hold its own data members and member function which can be accessed and user by creating an instance of that class and we say that class is building block that leads to Object Oriented programming 

Data member -  It is the data variable of the function 
Member function - It are the function use to manipulate these function variable 

For Example - 

A car is an object the car has attribute such as weight , color and methods such as drive and brake

Attribute and method are basically variable and function that belong to class

syntax -
class class{       // The class
  public:             // Access specifier
    int Num;        // Attribute (int variable)
    string word;  // Attribute (string variable)
};

Program -

// class explaination

#include<iostream>
using namespace std;

class classname
{
    public:
    string mystring;

    void printfun()
    {
        cout << "Geekname is : " << mystring;
    }
};

int main() {
    classname obj1;

    // accessing datamember
    obj1.mystring = "Hello world";

    // accessing member funcation
    obj1.printfun();
    return 0;
}


 Object in C language -

C++ is an object-oriented programming language. Everything in C++ is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake



Program -

// Object in c++
#include<iostream>
using namespace std;

class Classname{        // class 
    public:         //Access specifier
    int num;        //Attrbute (int variable)
    string mystring; //Attribute(String variable)
};
int main()
{
    Classname obj; // create an object of myclass

    //Access attribute and set value 
    obj.num = 12;
    obj.mystring = "hello string";
    
    // print attribute values
    cout<< "Number is " <<obj.num << endl;
    cout<< "String is " <<obj.mystring << endl;
    return 0;
}

/* Output - 
    Number is 12
    String is hello string */


There are two for defining class -

1. Inside class definition
2. Outside class definition


Program -
// Inside or outside class defination 

#include<iostream>
using namespace std;

class classname{
    public:
    string name;
    int id;

    // printname is not defined inside class defination
    // Outside class definition
    void printname();

    // printid is defined inside class defination
    void printid(){
        cout<<"Class id is = "<< id ;

    }
};

// Defination of printname using Scope resolution operator (::)

void classname::printname()
{
    cout << "Class name is : "<< name;
    
}

int main(){
    classname obj1;
    cout<<"Enter the name : ";
    cin>>obj1.name;
    obj1.id=15;

    obj1.printname();
    cout<< endl;
    obj1.printid();
    return 0;
}

/* Output -
Enter the name : name
Class name is : name
Class id is = 15
*/
    

How Function overloading work -

Function Overloading :-

Using of funcation in different place in program with same name or the process of  have two or more function with the same name but parameter are different called function overloading.

Simple program of function overloading -


#include<iostream>
using namespace std;
void integer(int a){
    cout<<"Here is int" << a <<endl;
}
void decimal(float b){
    cout<<"here is float"<< b <<endl;
}


int main(){
    integer(10);
    decimal(10.10);
    
    return 0;
}

// output -
// Here is int 10
// here is float 10.1


Function overloading with return type -

#include<iostream>
using namespace std;

class addition{
    public:
    int sum(int n1, int n2) {
        return n1+n2;
    }
    int sum(int n1, int n2, int n3) {
        return n1+n2+n3;
    }
};

int main(){
    addition add;
    cout<<add.sum(2,4)<<endl;
    cout<<add.sum(2,4,5);
    return 0;
}

// Output -
// 6
// 11

Function Overriding program - 

// Function Overriding 
#include<iostream>
using namespace std;

class BaseClass{
    public:
    virtual void Display()
    {
        cout<< "\n This is Display() method"
            "of BaseClass";
    }
    void show()
    {
        cout<<"\nThis is show() method"
            "of BaseClass";
    }
};

class DerivedClass : public BaseClass
{
    public:
    void Display()
    {
        cout<<"\nThis is Display() method"
            "of DerivedClass";
    }
};

// Driver code 
int main()
{
    DerivedClass dr;
    BaseClass &bs = dr;
    bs.Display();
    dr.show();
    
}

//Output-
// This is Display() method of Derived Class //This is show() method of Base Class


Accessing data member -

Public data member are also accessed  in the same way given however private data member are not allowed to accessed directly by object. accessing data member depends solely on access control of that data member 

there are three type of ADM -
1. Public 
2. Private
3. Protected








Comments

Popular posts from this blog

Funcation in c++ programming example in detail

Friend Funcation- 1. Adding to complex number in friend function #include<iostream> using namespace std; class container { float a,b; public: void get() { cout<<endl<<"Enter value for A="; cin>>a; cout<<endl<<"Enter value for B="; cin>>b; } void disp() { cout<<endl<<a; cout<<endl<<b; } friend container addition(container,container); }; container addition(container X,container y) { container Z; Z.a=X.a+y.a; Z.b=X.b+y.b; return Z; } int main() { container P,Q,R; P.get(); P.disp(); Q.get(); Q.disp(); R=addition(P,Q); R.disp(); return 0; } Output - Enter value for A=12 Enter value for B=12 12 12 Enter value for A=1 Enter value for B=1 1 1 13 13 2.  If class one is friend function of two it not means that two is friend functio...

C++ programming tutorial 2

Control statement in C++ PROGRAMMING - 1. if-else statement :    there are types below of if else statement so they are -     1. if-statement      In this if statement a condition was entered if the condition was match or say what we write in condition on it was true then program was run if it was false then the program in if statement was not run . Syntax - if(Condition){ //code you want to run } PROGRAM - // if-else statement  #include <iostream> using   namespace  std; int   main () {      int   a  ;      cout << "If the entered integer in equal to the condition they it run " << endl ;      cin >>   a ;      if ( a <= 6 )     {          cout << "Program was running" ...

C++ programming tutorial 5

 Call by value or reference - There are two type in function call by value or call by reference in it we pass the value in call by value we pass value and in call by reference we pass the reference of the value . Call by value in C++ Programming language  :- In call by value we pass the variable or say argument and then it used by function which we made so see the code given below  Syntax - #include <iostream > using namespace std; // making function  void fun_name(int a, int b) { ... } int main() { //calling function  fun_name(int n,int n1); return 0; } Code -  // function call by value in C++ programming #include <iostream> using namespace std; // call by value in function declaration void container(int a, int b); int main() {   int x,y;   cout<<"Enter the value of x :"<<endl;   cin>>x;   cout<<"Enter the value of y :"<<endl;   cin>>y;   // here we passing the value as argume...