C++ Lecture-3

You might also like

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

01

02

LECTURE 3
04
Today’s Agenda
01 Developing The First Object Oriented Program

02 Syntax Of Member Function

03
First C++ Program With Object Oriented Approach

06
04 Assignments?
Developing The First Object Oriented Application

 Developing the first OOP Program :


Declaration Of The Class
#include <iostream.h>
#include<conio.h>
Name Of The Class
class Student
{
Access specifier and since it is the default
private:
pl and define e<
Access specifier so we can avoid it
int roll;
char grade; Private Data Members
float per; OR
public: Private Instance Variable
void get(); Public Member Function
void show();
}; Access Specifier and this needs to be mentioned so that
We can call these member functions from outside the class
Syntax Of Member Function
 Syntax of defining member function :

<return type> <class_name> : : <mem_fn_name>(<arg>)


{
//body of the member function
}
pl and define e<
 Syntax of calling a member function:

<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.

Following is the rule for calculating salary:


1. Upto 40 hrs, the salary will be rph*hrswrkd.
2. For hours more than 40, the rph will be doubled.
End of Lecture 3
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like