0% found this document useful (0 votes)
45 views

Object Oriented Programming Class 2

The document discusses object oriented programming concepts in C++ including classes, objects, constructors, and access specifiers. It defines a class as a blueprint for creating objects with data fields and methods. Objects are instances of classes that encapsulate both data and functions. Constructors initialize objects when they are created and can be default, parameterized, or copy constructors. Access specifiers like public and private control access to class members.

Uploaded by

Saqlain Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Object Oriented Programming Class 2

The document discusses object oriented programming concepts in C++ including classes, objects, constructors, and access specifiers. It defines a class as a blueprint for creating objects with data fields and methods. Objects are instances of classes that encapsulate both data and functions. Constructors initialize objects when they are created and can be default, parameterized, or copy constructors. Access specifiers like public and private control access to class members.

Uploaded by

Saqlain Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Object Oriented Programming

Using C++
Object Oriented Programming (OOP):
• Object Oriented Programming (OOP) is a
programming technique in which programs are
written on the basis of objects.
• Object:
• An Object is a collection of data and functions.
• Object represent a person, place or thing in real
world.
• In oop data and all possible function on data are
grouped together.
Object Oriented Programming (OOP):
• Object Oriented programming is a programming
style that is associated with the concept of Class,
Objects and various other concepts revolving
around these two, like Inheritance,
Polymorphism, Abstraction, Encapsulation etc.
Class
• A class is an entity that determines how an
object will behave and what the object will
contain.
• In other words, it is a blueprint or a set of
instruction to build a specific type of object.
• The classes and objects are the most important
features of C++.
• A class is similar to structure but it provides
more advanced feature.
Classes(OOP)
• When we define a class we define a blueprint for
a data type.
• This doesn't actually define any data but it does
define what the class name means that is what
an object of the class will consist of and what
operations can be performed on such an object.
• A class definition starts with the keyword class
followed by the class name and the class body,
enclosed by a pair of curly braces.
• A class definition must be followed either by a
semicolon or a list of declarations.
Syntax
• class class_name{
field;
method;

};
Object(OOP)
• This is the basic unit of object oriented programming.
• That is both data and function that operate on data are
bundled as a unit called object.
• The main use of object to create instance of class
• The memory allocation for an object can be static or
dynamic. The static memory allocation is when an
object is declared directly without using any function
usually. And dynamic allocation is when we use some
dynamic allocation function to allocate memory for
data member of an object.
Declaring Object-Static Way
• Class_Name object_name:
Test T;

Dot Operator .
The dot operator connects Class object and member of that
class
T.Member_name;
Declaring Object-Static Way
#include<iostream>
using namespace std;
class test{
private:
int m;
public:
void getdata(){
cout<<"Enter No:";
cin>> m;
}
void display(){
cout<<m;
}
};
int main(){
test T;
T.getdata();
T.display();
return 0;
}
Declaring Object-Dynamic Way
Test *T=new Test();

#include<iostream>
using namespace std;
class test{
private:
int m;
public:
void getdata(){
cout<<"Enter No:";
cin>> m;
}
void display(){
cout<<m;
}
};
int main(){
test *p=new test();
p->getdata();
p->display();
return 0;
}
Class data and Member Function
• Access specifier label public and private
• Functions are public and data is private
• Data is hidden so that it can be safe from
accidental manipulation

• Functions operates on data are public so they


can be accessed from outside the class
Member Functions
• Member functions are the functions that operate
on the data encapsulated in the class
• Public member functions are the interface to the
class
• Define member function inside the class
definition
OR
• Define member function outside the class
definition(But they must be declared inside class definition)
Member Function
ReturnType function-name(){
……………
}
Member Functions inside the class body
class class_name{

public:
ReturnType function-name(){
……………
}

};
Example
#include<iostream>
using namespace std;
class Test{
private:
int n = 10;
public:
void show(){
cout<<"The valve of n:"<<n<<endl;
}
};
int main(){
Test T;
T.show();
return 0;
}
Member Functions Outside the class body
class class_name{
public:
ReturnType function-name();
};
ReturnType class_name::function-name(){
……………
}
The Scope Resolution Operator ::
The Scope Resolution Operator, the double colon,
is a token that allows access to static, constant,
and overridden properties or methods of a class.
When referencing these items from outside the
class definition, use the name of the class.
Example
#include<iostream>
using namespace std;
class Test{
private:
int n = 10;
public:
void show();
};
void Test::show(){
cout<<"The valve of n:"<<Test::n<<endl;
}
int main(){
Test T;
T.show();
return 0;
}
Access Specifier
Public
Privated
Protected
These access specifiers are used to set boundaries
for availability of members of class be it data
members or member functions
Access specifiers in the program, are followed by a
colon.
:
Access Specifier
Public:
Int x;
String X,Y;
Constructors
• C++ requires a construct call for each object it
has created
• Constructors are special class functions which
performs initialization of every object.
• The Compiler calls the Constructor whenever an
object is created.
• If there is no constructor, the compiler provides
a default constructor that is, a constructor with
no parameters
Constructors
• Constructor has the same name as the class itself
• Constructors do not return anything
• Constructors are automatically called when an
object is created
• Constructors are differ from other normal
member functions of a class.
Constructors
Three Types
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructors
class Test{
private:
int n;
public:
Test(){
n=2;
}

};
Parameterized Constructors
class Test{
private:
int n;
public:
Test(int b){
n=b;
}

};
Parameterized Constructors
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
 
class student
{
public:
string name,section;
int rollno;
Triangle(string a,string b,int c)
{
name = a;
section = b;
rollno = c;
}
void print()
{
cout<<“Student Name is:”<<name << endl;
cout<<“Student Sectionis:”<<section << endl;
cout<<“Student Roll number is:”<<rollno << endl;
}
};
int main(){
student T(“Ali”,”C”,1234);
T.print();
student st2(“Aslam”,”B”,4567);
st2.print();
return 0;
}
Semi Perimeter of Triangle
double s=(a+b+c)/2

b
a

You might also like