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;
}