Tag: Class in C++
Read a single sentence from user and display it.
by Binod on Apr.03, 2011, under C & C++
Objectives: Here we use cin.get() function to get a single sentence[including whitespace] as the input and display it to the O/P screen.
Source Code:
#include<iostream.h>
#include<conio.h>
class binod{
char ch[50];
public:
void getdata(){
cout<<”Enter a sentence:”;
cin.get(ch,50);
}
void showdata(){
cout <<”The sentence is: “<<ch;
}
};
main(){
binod b;
b.getdata();
b.showdata();
getch();
return 0;
}
Basic I/O operation in OOP using C++
by Binod on Apr.03, 2011, under C & C++
Objectives: Here we have Created a class name binod where there are two data member x & y [int] they are accessed by the member function getdata() and outputted by the next member function showdata(). Object ‘b’ is created.
Source Code:
#include<iostream.h>
#include<conio.h>
class binod{
int x,y; //private
public:
void getdata(){
cout<<”Enter x and y:”;
cin>>x>>y;
}
void showdata(){
cout<<”X=”<<x<<endl<<”Y=”<<y;
}
};
main(){
binod b;
b.getdata();
b.showdata();
getch();
return 0;
}
Illustration of simple class
by Binod on Dec.13, 2010, under C & C++
//illustration of simple class
#include<iostream.h>
#include<conio.h>
class binod{
int a, b,sum;
public:
void getdata() {
cout<<”Enter two numbers: “;
cin>>a>>b;
}
void showdata() {
cout<<”Sum of “<<a <<” and “<<b <<” is “<<a+b;
}
};
main()
{
binod obj;
obj.getdata();
obj.showdata();
getch();
return 0;
}