Introduction To Programming (C++) Classes and Objects
Introduction To Programming (C++) Classes and Objects
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
Once a class has been declared object can be created. For example
Item x;
Defining Member functions
7
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
<<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