SlideShare a Scribd company logo
1 of 7
Download to read offline
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
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
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++
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.
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();
}
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
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.

More Related Content

What's hot

Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 

What's hot (19)

Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Object and class
Object and classObject and class
Object and class
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 

Viewers also liked

Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2Abbas Ajmal
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++Learn By Watch
 

Viewers also liked (11)

Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
class and objects
class and objectsclass and objects
class and objects
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 

Similar to How to write you first class in c++ object oriented programming

classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.pptBArulmozhi
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Asfand Hassan
 
Virtual function
Virtual functionVirtual function
Virtual functionsdrhr
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2Aadil Ansari
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 

Similar to How to write you first class in c++ object oriented programming (20)

classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Oop
OopOop
Oop
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
Virtual function
Virtual functionVirtual function
Virtual function
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 

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.