First Program in C++
First Object Oriented Program in C++:
In C++ we can write object oriented programs as well as structured programs.
In this tutorial we are writing only object orieted programs.In object oriented programming we have to follow the following steps:
- Create class
- Create data fields or variables
- Define Member functions or functions
- Create objects of the class
- Use member functions of the class using objects
in object oriented programming.We need to create class first to find the solution of any problem.
first OOPs Program
//first oop program to display Hello Compuhelp
#include<iostream>
class First
{
public:
void message()
{
std::cout<<"Hello Compuhelp";
}
};//end of class
int main()
{
First first;//first is the object of class First
first.message();//calling member function
}//end of main
Code to write a class Student to input and display the data.
#include<iostream>
using namespace std;
class Student
{
char name[20];
int rollno;
float fee;
public:
void input()
{
cout<<"Enter name ";
cin>>name;
cout<<"Enter rollno ";
cin>>rollno;
cout<<"Enter fee ";
cin>>fee;
}
void display()
{
cout<<"\nName is "<<name;
cout<<"\nRollno is "<<rollno;
cout<<"\nEnter fee "<<fee;
}
};//end of class
int main()
{
Student st;
st.input();
st.display();
}//end of main
Output:
Enter name compuhelp
Enter rollno 23
Enter fee 10000
Name is compuhelp
Rollno is 23
Enter fee 10000