How to write you first class in c++ object oriented programming
1. W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C " P a g e | 1
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
HOW TO WRITE
YOUR FIRST CLASS IN C++
with Object-Oriented Programming
2. W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C " P a g e | 2
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
ࢫ܍ᠨ܍᥇
with the name of GOD, who is Very Kind and Merciful
3. W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C " P a g e | 3
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
With keyword of "class" we declare class in c++ we need to declare a class before declaring
entry point. Every Class have its own function that is also called Members. Every function
will perform specific task. Class is basically collection of different function and Object
Oriented Programming is just a set of different classes. Great way to use and re-use a set of
code. No need to repeat code repeat functions in your project. less coding and great output
is basic functionality of Object Oriented Programming.
Syntax for example in C++
class name of class {
public:
members/ functions/ class variables
private:
members/ functions/ class variables
protected:
members/ functions/ class variables
}
After declaring a class we should create and object of that class. Object is basically simple
variable that contains all the functions/ members of a class with which it is related. when an
object is created class is loaded in memory all variables of class refreshed and ready to get
value and to process them.
Object is instance of an object. we explore our class via objects. good programmers should
create separate files that contains class. For Example if we want to process with data of
students via class and an object we have a file that contain class code for students. Then we
will include that file in our project and after including will create and object of class. In C++
4. W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C " P a g e | 4
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
just write the name of class and after space write anything as a name of object and your
object is declared with specific class.
students myObj;
In above line students is a name of class and myObj is object variable of students Class.
the following code shows that how to create a simple class with three private variables and
three public members/ functions. "cal" is a name of class.
void getinput() is function to get values as input from user.
void processinput() is public function to processes given value, calculate its sum and
output is stored in result variable.
void setoutput() is public function to display result as output on monitor for users.
after this we have an entry point class called main() default class of C++ and should be
present in all programs because controller cannot execute anything without entry point.
now our class is ready and we just need to create an object of class in between main class.
After creating object we are ready to call all class functions.
cla obj; for creating object. cal is class name and obj is object variable.
obj.getinput(): is to get input from user. for example users will enter 10 two time.
obj.processinput(); is to process given value, calculate sum, 10+10=20, and store it in result
variable.
obj.setoutput(); is to set output on screen for user with answer.
5. W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C " P a g e | 5
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
GET TWO VALUES FROM USER AND CALCULATE THEM WITH OBJECT-ORIENTED
PROGRAMMING PARADIGM
#include<iostream>
using namespace std;
//sum_value(int, int);
// my first CLASS to Get Values, Process Value, And Set Outout
class cal{
private:
int result, val1, val2;
public:
void getinput(){
cout<<"Provide Me First Number ";
cin>>val1;
cout<< "Provide Me Second Number";
cin>>val2;
}
void processinput(){
result = val1 + val2;
}
void setoutput(){
cout<<"nnnnThe Sum of Given Two Numbers Is "<<result;
}
};
// end of Class
int main(int){
cal obj;
obj.getinput();
obj.processinput();
obj.setoutput();
}
6. W r i t e Y o u r F i r s t P r o g r a m w i t h
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/sy
SHOT OF
W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C "
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
SHOT OF DEV-CPP IDE AND OUTPUT
P a g e | 6
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
7. W r i t e Y o u r F i r s t P r o g r a m w i t h O O P I n " C " P a g e | 7
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
Thanks for being with me
need your prayers.