Example 1: C++ program to demonstrate the use of class and object
gd.cpp
#include <iostream>
using namespace std;
class gd
{
public:
    void add()
    {
        cout<<"The addition of 10+10="<<10 + 10<<"\n";
    }
};
int main()
{
    gd k;
    k.add();
    return 0;
}
Output
godarda@gd:~$ g++ gd.cpp
godarda@gd:~$ ./a.out The addition of 10+10=20 godarda@gd:~$
Example 2: C++ program to demonstrate the use of class and object
gd.cpp
#include <iostream>
using namespace std;
class gd
{
public:
    int add(int a, int b)
    {
        return a + b;
    }
};
int main()
{
    gd k;
    cout<<k.add(10, 10)<<endl;
}
Output
godarda@gd:~$ g++ gd.cpp
godarda@gd:~$ ./a.out 20 godarda@gd:~$
Example 3: C++ program to demonstrate the use of class and object
gd.cpp
#include <iostream>
using namespace std;
class gd
{
public:
    void add();
};
void gd::add()
{
    cout<<"The addition of 10+10="<<10 + 10<<"\n";
}
int main()
{
    gd k;
    k.add();
    return 0;
}
Output
godarda@gd:~$ g++ gd.cpp
godarda@gd:~$ ./a.out The addition of 10+10=20 godarda@gd:~$
Example 4: C++ program to demonstrate the use of class and object
gd.cpp
#include <iostream>
using namespace std;
class gd
{
private:
    int a, b;

public:
    void getdata();
    void add();
};
void gd::getdata()
{
    cout<<"Enter the 1st number ";
    cin>>a;
    cout<<"\nEnter the 2nd number ";
    cin>>b;
}
void gd::add()
{
    if (b < 0)
    {
        cout<<"\nThe addition of "<<a<<b<<" = "<<(a + b)<<"\n";
    }
    else
    {
        cout<<"\nThe addition of "<<a<<"+"<<b<<" = "<<(a + b)<<"\n";
    }
}
int main()
{
    gd k;
    k.getdata();
    k.add();
    return 0;
}
Output
godarda@gd:~$ g++ gd.cpp
godarda@gd:~$ ./a.out Enter the 1st number 10 Enter the 2nd number -50 The addition of 10-50 = -40 godarda@gd:~$
Comments and Reactions