Professional Documents
Culture Documents
C++ Lecture-3
C++ Lecture-3
C++ Lecture-3
02
LECTURE 3
04
Today’s Agenda
01 Developing The First Object Oriented Program
03
First C++ Program With Object Oriented Approach
06
04 Assignments?
Developing The First Object Oriented Application
<obj_name> <mem_fn_name>();
First Program In OOP
#include <iostream.h> { S.get();
#include<conio.h> cout<<“Enter roll,grade and per”; S.show();
cin>>roll>>grade>>per;
Class Student } Scope Resolution getch();
{ Operator
Private: void Student : : show(); Return 0;
int roll; Private Data { }
char grade; Member cout<<“Roll=”<<roll<<endl;
float per; cout <<“Grade=“<<grade<<endl;
cout<<“Per=“<<per<<endl;
public: Public } 10 R
Get
void get(); Mem-ber
Fn. S “A” G
void show(); Int main() Show
}; { 71.3 P
clrscr();
void Student : : get() Student S;
First Program In OOP
When we create 2 objects of same class:
#include <iostream.h> void Student : : get() clrscr();
#include<conio.h> { Student S P;
cout<<“Enter roll,grade and per”; S.get();
Class Student cin>>roll>>grade>>per; S.show(); 10 R
{ } P.get()
“A” G
private: P.show()
int roll; void Student : : show() getch(); 71.3 P
char grade; {
float per; cout<<“roll=”<<roll<<endl; Return 0;
cout <<“grade=“<<grade<<endl; } Get R
public: cout<<“per=“<<per<<endl; S
void get(); } Show G
void show(); P
}; Int main() P
{
Assignments
write an Object and Oriented Program to calculate and print the sum of two
integers given by the user : Cout<<“Nos are”<<a<<“and”<<b<<endl;
#include <iostream.h> void Num : : get() Cout<<“Their sum is”<<c;
#include <conio.h> { }
cout<<“Enter 2 int:”;
Class Num cin>>a>>b; Int main(); Output:
{ } { clrscr();
int a; pl and defineNum
e< obj; Enter 2 int: 10 20
int b; void Num : : add() obj.get(); Nos are 10 and 20
int c; { obj.add(); Their sum is 30
public: c=a+b; obj.show();
void get(); }
void add(); void Num : :show() getch();
void show(); { return 0;
}; }
Assignments
Q no.01 WAP to create a class call Circle having following members :
a. radius: an int member for storing radius of Circle.
b. area: a float member for storing area of the Circle.
c. circumf: a float member for storing circumference of the Circle.
Also provide following member fn:
a. get(): For accepting radius from the user.
b. calarea(): For calculate area of the Circle.
c. calcircumf(): For calculate the circumference of the Circle.
d. show(): For display area and circumference of the Circle.
Then write the function main(),create an object of Circle class and call the
Member function in appropriate order.
Assignments
Q no 02. WAP to create a class call Worker having following data members :
a. hrswrkd: For storing number of hours the worker has worked.
b. rph: For storing the rate per hour of the worker .
c. sal: For storing the salary of the worker.
Also provide following member fn:
a. get(): For accepting hours worked and rate per hour.
b. calculatesal(): For calculating salary of the worker.
d. show(): For displaying all the details of the worker.
Thank you