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

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 argument   // and also calling the function  

Tutorial 10 text align , decoration etc explaining

 Text in Css - 1.Text color -     here we can set color in text for making them more enhancive or if we want required to change in color of text it work perfectly  syntax - color : red; OUTPUT - text color is change to red 2.Text alignment -     It is used for align the text according to the requirement and their situation we align text at center , right , left etc syntax - text-align:center; 3.Text decoration-     It is to decorate the text let if we want to underline in text then we use text decoration and make it on the line  syntax - text-decoration: underline; OUTPUT - text have an underline decoration 4.Text transformation -  In this we transform letters into uppercase , lowercase and capitalization means if the letter is small then it will convert into uppercase or visa-versa syntax - text-transform:(uppercase , lowercase , capitalize); 5.Text spacing - It decided the space between letter and words both how many gap are between an letters and word but syntax for both is diffe