SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Object oriented Programming Using C++
Class ,Object
S.D.Gavarskar
Class Programming
Class is user defined data types, while an object is an instance of a class template.A
class provides a template,which defines the member functions and variables that are
required for the objects of class
Syntax
Class user_defined name
{
Private: // data member
//function member
Public : // data member
//function member
Accessing a
member of class
Class sample {
Private:
Intx;inty;
Public:
Int sum( );
Int diff( );
};
Void main( )
{sample one;
one.sum( );
one.diff( );
}
Example
Write a program to assign data members of class such as day,month,year and to
display the content of the class on screen
#include <iostream>
using namespace std;
int main() {
class date
{
public:
int day;
int month;
int year;
};
class date today;
today.day=01;
today.month =03;
today.year=2023;
cout << “Today date is = ”<< today.day << "/" ;
cout << today.month <<"/" << today.year<< endl;
return 0;
}
Output :
Today date is = 1/3/2023
Write a program to demonstrate how to define both data member and member
function of a class within the scope of class definition
#include <iostream>
using namespace std;
class date
{
public:
int day;
int month;
int year;
public :
void getdata(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
void display(void)
{
cout << “Today date is = ”<< day << "/" ;
cout << month <<"/" << year<< endl;
}
};
int main() {
class date today;
int d1,m1,y1;
d1=01;
m1 =03;
y1=2023;
today.getdata(d1,m1,y1) ;
today.display();
return 0;
}
Output:
Today date is = 1/3/2023
Write a program to display entered name is correct or not
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// Declaring a string object
string str;
string str1;
str1="sushama";
cout << "Enter a string: ";
getline(cin, str);
if (str==str1)
cout << "Entered name is correct " << endl;
else
cout << " Entered name is not correct ";
return 0;
}
Output:
Enter a string: sushama
Entered name is correct
Write a program to display your name using c++
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// Declaring a string object
string str;
string str1;
string str2;
str1="sushama";
cout << "Enter First Name : ";
getline(cin, str);
cout << "Enter Middle name : ";
getline(cin, str1);
cout << "Enter Middle name : ";
getline(cin, str2);
cout << "Employ name is "<< endl;
cout << str << str1 << str2 << endl;
}
Enter First Name : Sushama
Enter Middle name : Dattaray
Enter Middle name : Gavarskar
Employ name is
SushamaDattarayGavarskar
Write a program to display entered name is correct or not (Using class)
// Online C++ compiler to run C++ program online
#include <iostream>
#include <string.h>
using namespace std;
class name
{
public :
string st;
string str;
public :
void display(string st1)
{
st=st1;
str="sushama";
if (st==str)
cout << "Entered name is correct " << endl;
else
cout << " Entered name is not correct ";
}
};
int main() {
{
// Declaring a string object
class name obj;
string str2;
cout << "Enter a string: ";
getline(cin, str2);
obj.display(str2);
return 0;
}
}
Output :Enter a string: kavita
Entered name is not correct
Here class
name is
name
Display
is
function
of public
type
Under class
template
object is
created
named as objt
Call display
function in obj
Write a program to display entered name is correct or not (Using class) Declare function as
private
#include <iostream>
#include <string.h>
using namespace std;
class name
{
public :
string st;
string str;
private :
void display(string st1)
{
st=st1;
str="sushama";
if (st==str)
cout << "Entered name is correct " << endl;
else
cout << " Entered name is not correct ";
}
};
int main() {
{
// Declaring a string object
class name obj;
string str2;
cout << "Enter a string: ";
getline(cin, str2);
obj.display(str2);
return 0;
}
}
Output :
/tmp/SZGXN7Mc1h.cpp:31:12: error: 'void name::display(std::string)' is private
within this context
31 | obj.display(str2);
| ~~~~~~~~~~~^~~~~~
/tmp/SZGXN7Mc1h.cpp:13:10: note: declared private here
13 | void display(string st1)
| ^~~~~~~
Difference between public and private
Public Private
All the class members declared under public will
be available to everyone.
The class members declared as private can be
accessed only by the functions inside the class.
The data members and member functions
declared public can be accessed by other classes
too.
Only the member functions or the friend
functions are allowed to access the private data
members of a class.
The public members of a class can be accessed
from anywhere in the program using the direct
member access operator (.) with the object of that
class.
They are not allowed to be accessed directly by
any object or function outside the class.
Problem statement .
Write a program by creating an 'Employee' class having the following functions and print the final salary.
1 - 'getInfo()' which takes the salary, number of hours of work per day of employee as parameters (per
hour $10)
2 - 'AddSal()' which adds $500 to the salary of the employee if it is less than $500.which adds $5 per hour
to the salary of the employee if the number of hours of work per day is more than 6 hours
Code …
#include <iostream>
// Online C++ compiler to run C++ program online
using namespace std;
class Employee
{
int salary;
int nodays;
int nohours;
int h1;
int d1;
public:
void getinfo (float d,float h)
{
int salary;
nodays=d;
nohours=h;
d1=nohours/nodays;
h1=nohours % nodays;
;
cout<< " working hours per day"<< d1<< endl;
cout << " Extra working hours = "<< h1<<endl;
}
int addsal()
{
if (h1!=00)
{
salary=(nodays*10)+(h1*5);
if(salary<500)
return (salary+500);
else
return (salary);
}
else
{
if (h1== 00 )
{
salary = nodays*10;
if (salary <500)
return (salary+500);
else
return (salary);
}
}
}
};
Cont…
int main()
{
Employee obj1;
int days;
int hours;
cout<< "Enter no of days worker works "<< endl;
cin>> days;
cout<< "Enter no of hours worker works "<<
endl;
cin>> hours;
obj1.getinfo (days,hours);
cout <<"salary of employee is = " <<
obj1.addsal();
return 0;
}
Output
Enter no of days worker works
20
Enter no of hours worker works
126
working hours per day6
Extra working hours = 6
salary of employee is = 730
Output
Enter no of days worker works
20
Enter no of hours worker works
120
working hours per day6
Extra working hours = 0
salary of employee is = 700
Write a program that would print the information (name, year of joining, salary, address) of
three employees by creating a class named 'Employee'. The output should be as follows:
Name Year of joining Address
Robert 1994 64C- WallsStreat
Sam 2000 68D- WallsStreat
John 1999 26B- WallsStreat
Code
#include <iostream>
#include <string.h>
using namespace std;
class Employee
{
int salary;
string name;
int Year;
string address;
public:
void getin ()
{
cout<< " enter name of Employee"<< endl;
getline (cin,name);
cin.clear();
cout<< " enter address of Employee"<< endl;
getline (cin,address);
cin.clear();
cout<< " enter date of joining of Employee"<< endl;
cin >> Year;
}
void display ()
{
cout << name << " " << Year << " " << address << endl;
}
};
i
nt main()
{
Employee obj1[10];
int i;
cout << "Enter information of Employee"<<endl;
//cout << "Name" << "Year of Joining" << "Address" << endl;
for (i=0;i<=2;i++)
{
obj1[i].getin();
}
for (i=0;i<=2;i++)
{
obj1[i].display();
}
return 0;
}
Output :

Weitere ähnliche Inhalte

Ähnlich wie OOPS 22-23 (1).pptx

Ähnlich wie OOPS 22-23 (1).pptx (20)

OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
Overloading
OverloadingOverloading
Overloading
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Classes(or Libraries)#include #include #include #include.docx
Classes(or Libraries)#include #include #include #include.docxClasses(or Libraries)#include #include #include #include.docx
Classes(or Libraries)#include #include #include #include.docx
 
Bc0037
Bc0037Bc0037
Bc0037
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Lecture5
Lecture5Lecture5
Lecture5
 
ccc
cccccc
ccc
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 

Kürzlich hochgeladen

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 

Kürzlich hochgeladen (20)

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 

OOPS 22-23 (1).pptx

  • 1. Object oriented Programming Using C++ Class ,Object S.D.Gavarskar
  • 2. Class Programming Class is user defined data types, while an object is an instance of a class template.A class provides a template,which defines the member functions and variables that are required for the objects of class Syntax Class user_defined name { Private: // data member //function member Public : // data member //function member
  • 3. Accessing a member of class Class sample { Private: Intx;inty; Public: Int sum( ); Int diff( ); }; Void main( ) {sample one; one.sum( ); one.diff( ); } Example
  • 4. Write a program to assign data members of class such as day,month,year and to display the content of the class on screen #include <iostream> using namespace std; int main() { class date { public: int day; int month; int year; }; class date today; today.day=01; today.month =03; today.year=2023; cout << “Today date is = ”<< today.day << "/" ; cout << today.month <<"/" << today.year<< endl; return 0; } Output : Today date is = 1/3/2023
  • 5. Write a program to demonstrate how to define both data member and member function of a class within the scope of class definition #include <iostream> using namespace std; class date { public: int day; int month; int year; public : void getdata(int d,int m,int y) { day=d; month=m; year=y; } void display(void) { cout << “Today date is = ”<< day << "/" ; cout << month <<"/" << year<< endl; } }; int main() { class date today; int d1,m1,y1; d1=01; m1 =03; y1=2023; today.getdata(d1,m1,y1) ; today.display(); return 0; } Output: Today date is = 1/3/2023
  • 6. Write a program to display entered name is correct or not #include <iostream> #include <string.h> using namespace std; int main() { // Declaring a string object string str; string str1; str1="sushama"; cout << "Enter a string: "; getline(cin, str); if (str==str1) cout << "Entered name is correct " << endl; else cout << " Entered name is not correct "; return 0; } Output: Enter a string: sushama Entered name is correct
  • 7. Write a program to display your name using c++ #include <iostream> #include <string.h> using namespace std; int main() { // Declaring a string object string str; string str1; string str2; str1="sushama"; cout << "Enter First Name : "; getline(cin, str); cout << "Enter Middle name : "; getline(cin, str1); cout << "Enter Middle name : "; getline(cin, str2); cout << "Employ name is "<< endl; cout << str << str1 << str2 << endl; } Enter First Name : Sushama Enter Middle name : Dattaray Enter Middle name : Gavarskar Employ name is SushamaDattarayGavarskar
  • 8. Write a program to display entered name is correct or not (Using class) // Online C++ compiler to run C++ program online #include <iostream> #include <string.h> using namespace std; class name { public : string st; string str; public : void display(string st1) { st=st1; str="sushama"; if (st==str) cout << "Entered name is correct " << endl; else cout << " Entered name is not correct "; } }; int main() { { // Declaring a string object class name obj; string str2; cout << "Enter a string: "; getline(cin, str2); obj.display(str2); return 0; } } Output :Enter a string: kavita Entered name is not correct Here class name is name Display is function of public type Under class template object is created named as objt Call display function in obj
  • 9. Write a program to display entered name is correct or not (Using class) Declare function as private #include <iostream> #include <string.h> using namespace std; class name { public : string st; string str; private : void display(string st1) { st=st1; str="sushama"; if (st==str) cout << "Entered name is correct " << endl; else cout << " Entered name is not correct "; } }; int main() { { // Declaring a string object class name obj; string str2; cout << "Enter a string: "; getline(cin, str2); obj.display(str2); return 0; } } Output : /tmp/SZGXN7Mc1h.cpp:31:12: error: 'void name::display(std::string)' is private within this context 31 | obj.display(str2); | ~~~~~~~~~~~^~~~~~ /tmp/SZGXN7Mc1h.cpp:13:10: note: declared private here 13 | void display(string st1) | ^~~~~~~
  • 10. Difference between public and private Public Private All the class members declared under public will be available to everyone. The class members declared as private can be accessed only by the functions inside the class. The data members and member functions declared public can be accessed by other classes too. Only the member functions or the friend functions are allowed to access the private data members of a class. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class. They are not allowed to be accessed directly by any object or function outside the class.
  • 11. Problem statement . Write a program by creating an 'Employee' class having the following functions and print the final salary. 1 - 'getInfo()' which takes the salary, number of hours of work per day of employee as parameters (per hour $10) 2 - 'AddSal()' which adds $500 to the salary of the employee if it is less than $500.which adds $5 per hour to the salary of the employee if the number of hours of work per day is more than 6 hours
  • 12. Code … #include <iostream> // Online C++ compiler to run C++ program online using namespace std; class Employee { int salary; int nodays; int nohours; int h1; int d1; public: void getinfo (float d,float h) { int salary; nodays=d; nohours=h; d1=nohours/nodays; h1=nohours % nodays; ; cout<< " working hours per day"<< d1<< endl; cout << " Extra working hours = "<< h1<<endl; } int addsal() { if (h1!=00) { salary=(nodays*10)+(h1*5); if(salary<500) return (salary+500); else return (salary); } else { if (h1== 00 ) { salary = nodays*10; if (salary <500) return (salary+500); else return (salary); } } } };
  • 13. Cont… int main() { Employee obj1; int days; int hours; cout<< "Enter no of days worker works "<< endl; cin>> days; cout<< "Enter no of hours worker works "<< endl; cin>> hours; obj1.getinfo (days,hours); cout <<"salary of employee is = " << obj1.addsal(); return 0; } Output Enter no of days worker works 20 Enter no of hours worker works 126 working hours per day6 Extra working hours = 6 salary of employee is = 730 Output Enter no of days worker works 20 Enter no of hours worker works 120 working hours per day6 Extra working hours = 0 salary of employee is = 700
  • 14. Write a program that would print the information (name, year of joining, salary, address) of three employees by creating a class named 'Employee'. The output should be as follows: Name Year of joining Address Robert 1994 64C- WallsStreat Sam 2000 68D- WallsStreat John 1999 26B- WallsStreat
  • 15. Code #include <iostream> #include <string.h> using namespace std; class Employee { int salary; string name; int Year; string address; public: void getin () { cout<< " enter name of Employee"<< endl; getline (cin,name); cin.clear(); cout<< " enter address of Employee"<< endl; getline (cin,address); cin.clear(); cout<< " enter date of joining of Employee"<< endl; cin >> Year; } void display () { cout << name << " " << Year << " " << address << endl; } }; i nt main() { Employee obj1[10]; int i; cout << "Enter information of Employee"<<endl; //cout << "Name" << "Year of Joining" << "Address" << endl; for (i=0;i<=2;i++) { obj1[i].getin(); } for (i=0;i<=2;i++) { obj1[i].display(); } return 0; }