Introduction To Programming (C++) Classes and Objects

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

1

Introduction to Programming [C++]


Lecture 05:
Classes and Objects
Learning Objectives
2

To know about:
Class and Object
Member Data and Member Function
Access Modifier
Class Declaration and Creating Objects
Defining Member Function
Get and Set method
Array of Objects
Memory Allocation
Class and Object
3

Class: A class is a way of binding data & its


associated functions together.
Object: An object is an instantiation of a class. In
terms of variables, a class would be the type, and an
object would be the variable.
Member data and member function
4

• Data of a class are called member data


• Functions of a class are called member
function
Access Modifiers
5

A private member is a member which is a


accessible only within the class.
A public member is a member which is
-Accessible within the class.
-Accessible from outside the class by using
an object of that class & the dot(.)
operator.
By default the member of a class are private.
Usually the data member are kept as private &
member functions are kept as public.
Class Declaration and Creating Objects
6

A typical class declaration would look like


class Item{
public:
int a; //variable declaration
int b;

int displayA(); //function declaration


int displayB();
};

Once a class has been declared object can be created. For example

Item x;
Defining Member functions
7

Member functions can be defined in two places


• Outside the class definition
• Inside the class definition
Outside the class definition
8
#include<iostream> int Item::displayA(){
using namespace std; return a;
}
class Item{ int Item::displayB(){
public: return b;
int a=12; }
int b=13; int main(){
Item w;
cout<<w.displayB();
int displayA();
return 0;
int displayB();
}
};
Inside the class definition
9
#include<iostream> int main(){
using namespace std; Item w;
cout<<w.displayB();
class Item{ return 0;
public: }
int a=12;
int b=13;
int displayA(){

return a;
}
int displayB(){

return b;}};
get method and set method
10
#include<iostream> void getData(){
using namespace std; cout<<a<<endl;
cout<<b<<endl;
class Item{ }
private: };
int a;
int b; int main(){
public: Item w;
void setData(int x,int y){ w.setData(12,13);
a=x; w.getData();
b=y; return 0;
} }
Array of Objects
11
/*Write a program that can take input int main(){
information Student s1[2];
of 2 students(ID,CGPA) int i;
and also show the stored information for(i=0;i<2;i++){
on console cout<<"Student 1 ID: "<<endl;
-*/ cin>>s1[i].ID;
#include<iostream> cout<<"Student 1 CGPA:"<<endl;
using namespace std; cin>>s1[i].CGPA;
class Student{ cout<<s1[i].ID<<endl;
//public: cout<<s1[i].CGPA<<endl;
int ID; }
float CGPA; }

};
Memory Allocation
12

For each object of a class, memory is


allocated separately
Memory is allocated for objects, when
objects are declared
More example programs
13
class item int main()
{
{
private:
int id; // member data item t1;
double price; t1.getdata();
char *name; t1.putdata();
public: return 0;
void getdata() //member function }
{
name=new char[50];
cin>>id>>price>>name;
}
void putdata()
{
cout<<"id="<<id<<"price=“

<<price<<"name="<<name<<en
dl;
}
};
More example programs
14
class set int main()
{ {
int m,n; set s;
public: s.setdata(5,10);
void setdata(int x,int y) s.largest();
{ s.show(5);
m=x,n=y; return 0;
} }
void show(int x)
{
cout<<x<<endl;
}
void largest()
{
if(m>n) show(m);
else show(n);
}
};
15

THANK YOU

You might also like