OOPS notes pdf including objects, Lecture notes of Object Oriented Programming

Best oops notes for colleges includes class

Typology: Lecture notes

2018/2019

Uploaded on 05/24/2019

abhinav-monga
abhinav-monga šŸ‡®šŸ‡³

4

(1)

1 document

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROGRAM 1:
A class student has three data members: name, roll, marks of 5 subjects and
member functions to assign streams on the basis of the table given below:
Average marks Stream
96% and more computer science
91% - 95% electronics
86% - 90% mechanical
81% - 85% electrical
76% - 80% chemical
71% - 75% civil
Declare the class student and define the member functions.
#include<iostream>
using namespace std;
class Student
{
char name[20];
int roll,marks[5];
public:
void input()
{
cout<<"Enter name : ";
cin>>name;
cout<<"Enter rollno : ";
cin>>roll;
cout<<"Enter marks : ";
for(int i=0;i<5;i++)
cin>>marks[i];
}
void stream()
{
float avg;
avg=(marks[0]+marks[1]+marks[2]+marks[3]+marks[4])/5;
if(avg>=96) cout<<"Computer Science";
else if(avg>=91 && avg<=95) cout<<"Electronics";
else if(avg>=86 && avg<=90) cout<<"Mechanical";
else if(avg>=81 && avg<=85) cout<<"Electrical";
else if(avg>=76 && avg<=80) cout<<"Chemical";
else cout<<"Civil";
}
};
int main()
{ Student s;
s.input();
s.stream();
}
PROGRAM 2:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download OOPS notes pdf including objects and more Lecture notes Object Oriented Programming in PDF only on Docsity!

PROGRAM 1:

A class student has three data members: name, roll, marks of 5 subjects and

member functions to assign streams on the basis of the table given below:

Average marks Stream

96% and more computer science

91% - 95% electronics

86% - 90% mechanical

81% - 85% electrical

76% - 80% chemical

71% - 75% civil

Declare the class student and define the member functions.

#include using namespace std; class Student { char name[20]; int roll,marks[5]; public: void input() { cout<<"Enter name : "; cin>>name; cout<<"Enter rollno : "; cin>>roll; cout<<"Enter marks : "; for(int i=0;i<5;i++) cin>>marks[i]; } void stream() { float avg; avg=(marks[0]+marks[1]+marks[2]+marks[3]+marks[4])/5; if(avg>=96) cout<<"Computer Science"; else if(avg>=91 && avg<=95) cout<<"Electronics"; else if(avg>=86 && avg<=90) cout<<"Mechanical"; else if(avg>=81 && avg<=85) cout<<"Electrical"; else if(avg>=76 && avg<=80) cout<<"Chemical"; else cout<<"Civil"; } }; int main() { Student s; s.input(); s.stream(); }

PROGRAM 2:

Declare a class to represent bank account of 10 customers with the following

data members:

Name of depositor

Account number

Type of account (s for savings, c for current)

Balance amount

The class also contains the following member functions:

A. To initialize

B. To deposit money

C. For withdrawal if the deposit after withdrawal is greater than 10000

D. To display the data members

#include using namespace std;

class Account { char name[30]; int acno; char type; float balance; public: void initial(){ cout<<"Enter name: "; cin>>name; cout<<"Enter Account No: "; cin>>acno; cout<<"Enter account type: "; cin>>type; cout<<"Enter intial amount: "; cin>>balance; } void deposit(float amount){ balance = balance + amount; } void withdraw(float amount){ char opt; cout<<"Current balance: "<<balance<<endl; cout<<"Are you sure to withdraw? (y/n)"; cin>>opt; if(opt == 'y' ){ if(balance-amount >= 10000){ balance = balance - amount; }else{ cout<<"Insufficient balance! \a\a\a"; } }

return 0; }

PROGRAM 3:

Program to track the total number of objects of a class existing in a program.

#include using namespace std;

class test { int code; static int count;

public: void setcode(void) { code = ++count; } void showcode(void) { cout << "object number :" << code << "\n"; } static void showcount(void) { cout << "count:" << count << "\n"; } }; int test::count; int main() { test t1, t2; t1.setcode(); t2.setcode();

test::showcount();

test t3; t3.setcode();

test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return 0; }

PROGRAM 4:

Define a class serial with following specifications:

Private members:

Serial code integer

Title 20 character

Duration float

No. ofepisodes integer

Public member function of class serial:

1. A constructor to initialize duration as 30 and No. ofepisodes as 10

int main() { char ch='y'; int i=0,dur,no; serial s1[10]; while(ch=='y') { s1[i].newserial(); cout<<"Enter the duration and the no. of episodes:"; cin>>dur>>no; s1[i].otherentries(dur,no); i++; cout<<"\n\nDo you want to continue:"; cin>>ch; } cout<<"\n\nThe details you have entered are:"<<endl<<endl; for(int j=0;j<i;j++){ cout<<"Data of serial "<<j+1<<" is:"<<endl; s1[j].displaydata(); } return 0; }

PROGRAM 5:

Considering the following specifications:

Structure name

Name

First char[40]

Mid char[40]

Last char[60]

Structure name

Phone

Area char[4]

Exch char[4]

Numb char[6]

Class with name and p_rec as its data members as objects of structure name

and phone with member functions and constructor.

#include #include using namespace std;

struct Name { char First[40]; char Mid[40]; char Last[60]; };

struct Phone { char Area[40]; charExch[40]; char Numb[60]; }; class employee{ Name name; Phone p_rec; public: employee(){ strcpy(name.First,"\0"); strcpy(name.Mid,"\0"); strcpy(name.Last,"\0"); strcpy(p_rec.Area,"\0"); strcpy(p_rec.Exch,"\0"); strcpy(p_rec.Numb,"\0"); }

voidset_name(char *nf,char *nm,char *nl){ strcpy(name.First,nf); strcpy(name.Mid,nm); strcpy(name.Last,nl); } void set_val1(char *nf,char *nm,char *nl){ strcpy(p_rec.Area,nf); strcpy(p_rec.Exch,nm); strcpy(p_rec.Numb,nl); } void input(){ cout<<"enter first name\n"; cin.getline(name.First,40); cout<<"enter you mid name\n"; cin.getline(name.Mid,40); cout<<"enter your last name\n"; cin.getline(name.Last,60);

cout<<" Height : "<<height<<endl; cout<<" Width : "<<width<<endl; cout<<" Depth : "<<depth<<endl; } };

class Carton : public Box { int weight; public: void input_carton() { cout<<"Enter weight "; cin>>weight; } void display_carton() { cout<<"Carton : "; cout<<weight<<endl; } };

int main() {

Carton c; c.input_box(); c.input_carton(); cout<<"Displaying Details :\n"; c.display_box(); c.display_carton();

}

PROGRAM 7:

Demonstrate the use of scope resolution operator in case of redefined

members in a derived class.

#include using namespace std; class base { public: void print() { cout<<"This is a base class"; } };

class derived:public base { public:

switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: if(date==31) { date=1; month++; } else { date++; } break; case 4: case 6: case 9: case 11: if(date==30) { date=1; month++; } else { date++; } break; case 12: if(date==31) { date=1; month=1; year++; } else { date++; } break; case 2: if (year % 4 == 0) { if ((year % 100 == 0)&&(year % 400 == 0)) { if(date==29) {

date=1; month++; } else { date++; } } if(date==29) { date=1; month++; } else { date++; } } else { if(date==28) { date=1; month++; } else if(date==29) { cout<<"Invalid Date!"; exit(0); } else { date++; } } break; }

} void showdate() { cout<<"\nTomorrow will be "<<date<<"-"<<month<<"-"<<year; } };

int main() { Date d;

complex temp; temp.a=a+obj.a; temp.b=b+obj.b; return(temp); } int main() { complex c1(5,6),c2(7,8),c3; cout<<"The 1st no. is:"; c1.show(); cout<<"\nThe 2nd no. is:"; c2.show(); c3=c1+c2; cout<<"\nSum is:"; c3.show(); getch(); }

PROGRAM 10:

Write a program overloading assignment operator, and comparison operator

for the Box class.

#include using namespace std;

class box { public:

int l,b,h;

box(float a,float e,float c) { l=a; b=e; h=c; } void operator=(const box& d) { l=d.l; b=d.b; h=d.h; } bool operator<(const box& d) {

if((lbh)<(d.ld.bd.h)) return true; else return false; } bool operator <=(const box& d) { if((lbh)<=(d.ld.bd.h)) return true; else return false; } bool operator >(const box& d) { if((lbh)>(d.ld.bd.h)) return true; else return false; } bool operator >=(const box& d) { if((lbh)>=(d.ld.bd.h)) return true; else return false; } bool operator==(const box& d) { if((lbh)==(d.ld.bd.h)) return true; else return false; } };

int main() { box b1(2,3,4),b2(5,6,7); bool accept; cout<<"Before applying b1=b2\n"; if(b1<b2) cout<<"b1<b2"; if(b1>b2) cout<<"\nb1>b2"; cout<<"\nAfter applying b1=b2"; b1=b2; if(b1>=b2) cout<<"\nb1>=b2";

getch(); } int area(int s) { return(ss); } int area(int l,int b) { return(lb); } float area(float r) { return(3.14rr); } float area(float bs,float ht) { return((bs*ht)/2); }

PROGRAM 12:

Design a program for calculating the area of a triangle, rectangle and circle by

taking shape as the base class using virtual functions.

#include<iostream.h> #include<conio.h> class shape { public: double x,y; virtual void getdata()=0; virtual void area()=0; }; class circle:public shape { public: double are; int aa; void getdata() { cout<<"\nenter the radius"; cin>>aa; } void area() { are=(3.14 aaaa); } void display() {

cout<<"area of circle is"<< are; } }; class rectangle:public shape { public: double are; int aa,bb; void getdata() { cout<<"\nenter the length and breadth"; cin>>aa>>bb; } void area() { are=(aa*bb); } void display() { cout<<"area of rectangle is"<< are; }

}; class triangle:public shape { public: double are; int aa,bb; void getdata() { cout<<"\nenter the base and height of triangle "; cin>>aa>>bb; } void area() { are=(0.5bbaa); } void display() { cout<<"area of triangle is"<< are; } }; int main() { shape *s; circle c; triangle t; rectangle r; clrscr();