Example:2
Write a C++ program to declare a class named student with attributes as student code , student name and percentage of marks. Take input for the details and display them.
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
int roll;
char name[20];
float per;
public:
void read();
void show();
};
void student::read()
{
cout<<"Enter roll, name and per ";
cin>>roll>>name>>per;
}
void student::show()
{
cout<<"roll "<<roll<<" name "<<name<<" per "<<per<<endl;
}
int main()
{
//Normal object
student s1;
s1.read();
s1.show();
//pointer type object
student *p=new student;
p->read();
p->show();
return(0);
}
Output:
Enter roll, name and per 101
sumit
95
roll 101 name sumit per 95
Enter roll, name and per 102
mohit
99
roll 102 name mohit per 99
Example:3
Write a C++ program to declare a class named product with attributes as product number, product name, rate , quantity and cost. Take input for the details , calculate and print total cost of the propduct?
#include<iostream>
#include<conio.h>
using namespace std;
class product
{
private:
int pno;
char pname[20];
float rate,qty,cost;
public:
void read();
void cal();
void show();
};
void product::read()
{
cout<<"Enter pno, pname ,rate and qty ";
cin>>pno>>pname>>rate>>qty;
}
void product::cal()
{
cost=rate*qty;
}
void product::show()
{
cal();
cout<<"pno "<<pno<<" pname "<<pname<<" rate "<<rate<<" qty "<<qty<<" cost "<<cost<<endl;
}
int main()
{
//Normal object
product p1;
p1.read();
p1.show();
//pointer type object
product *p=new product;
p->read();
p->show();
return(0);
}
Output:
Enter pno, pname ,rate and qty 1001
Dove
45
6
pno 1001 pname Dove rate 45 qty 6 cost 270
Enter pno, pname ,rate and qty 1002
Surf
65
3
pno 1002 pname Surf rate 65 qty 3 cost 195




