Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

CPP program

The document contains multiple C++ code examples demonstrating key Object-Oriented Programming concepts such as encapsulation, abstraction, inheritance, polymorphism, constructor overloading, and operator overloading. It includes practical implementations for classes like Employee, Vehicle, and Student_Info, showcasing how to manage data and perform operations. Additionally, it illustrates the use of the scope resolution operator to differentiate between global and local variables.

Uploaded by

balaji
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CPP program

The document contains multiple C++ code examples demonstrating key Object-Oriented Programming concepts such as encapsulation, abstraction, inheritance, polymorphism, constructor overloading, and operator overloading. It includes practical implementations for classes like Employee, Vehicle, and Student_Info, showcasing how to manage data and perform operations. Additionally, it illustrates the use of the scope resolution operator to differentiate between global and local variables.

Uploaded by

balaji
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//Encapsulation

#include <iostream>
using namespace std;

class Employee {
private:
// Private attribute
int salary;

public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};

int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}

// C++ Program to Demonstrate the


// working of Abstraction
#include <iostream>
using namespace std;
class number
{
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};

int main()
{
number obj;
obj.set(10, 20);
obj.display();
return 0;
}
#include<iostream>
using namespace std;

class Vehicle
{
public:
void company()
{
cout<<"GFG\n";
}
public:
void model()
{
cout<<"SIMPLE\n";
}
public:
void color()
{
cout<<"Red/GREEN/Silver\n";
}
public:
void cost()
{
cout<<"Rs. 60000 to 900000\n";
}
public:
void oil()
{
cout<<"PETRO\n";
}
private:
void piston()
{
cout<<"4 piston\n";
}
private:
void manWhoMade()
{
cout<<"Markus Librette\n";
}
};
int main()
{

Vehicle obj;
obj.company();
obj.model();
obj.color();
obj.cost();
obj.oil();
}

#include <iostream>
using namespace std;
class Account {
private:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}

//Polymorphism Compile Time Polymorphism


//Function Overloading
#include <iostream>
using namespace std;
class add
{
public:
int sum(int n1, int n2)
{
return n1+n2;
}
int sum(int n1, int n2, int n3)
{
return n1+n2+n3;
}
};
int main()
{
add a;
cout<<"Output : "<<a.sum(10,20)<<endl;
cout<<"Output : "<<a.sum(10,20,30)<<endl;
return 0;
}

//Polymorphism - Run Time Polymorphisms


#include <iostream>
using namespace std;
class A
{
public:
void disp()
{
cout<<"Super Class Function"<<endl;
}
};
class B:public A
{
public:
void disp()
{
cout<<"Super Class Function"<<endl;
}
};
int main()
{
A a;
a.disp();
B b;
b.disp();
return 0;
}

//Constructor Overloading
#include <iostream>
using namespace std;
class rect
{
private:
int area;
public:
rect()
{
area=0;
}
rect(int a,int b)
{
area=a*b;
}
void disp()
{
cout<<"The area is "<<area<<endl;
}
};
main()
{
rect r1;
rect r2(2,7);
r1.disp();
r2.disp();
}

//Operator Overloading
#include<iostream>
using namespace std;
class test
{
private:
int n;
public:
test():n(8)
{

}
void operator ++()
{
n=n+2;
}
void print()
{
cout<<"The Output is "<<n<<endl;
}
};
int main()
{
test t;
++t;
t.print();
return 0;
}

#include <iostream>
using namespace std;
class Student_Info{
int roll_number;
char student_name[50], grade[2];

public:
void read_data(int count){
cout<<"\n\n--------- Enter student "<<count+1<<" information ---------\
n";
cout<<"Name of the Student (Max. 50 characters only): ";
cin>>student_name;
cout<<"Roll Number: ";
cin>>roll_number;
cout<<"Grade (O, A+, A, B+, B, C, D, F): ";
cin>>grade;
cout<<"\nStudent information with roll number "<<roll_number<<" has
saved!";
}
void display_data(int count){
cout<<"\n\n******** Student "<<count+1<<" Information ********";
cout<<"\nName of the Student: "<<student_name;
cout<<"\nRoll Number: "<<roll_number;
cout<<"\nGrade Secured: "<<grade;
cout<<"\n---------------------------------------\n";
}
};

int main(){
Student_Info stud[3];
int i;
for(i=0; i<3; i++)
stud[i].read_data(i);
cout<<"\n\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
cout<<"The information of 3 students has been saved.";
cout<<"\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
for(i=0; i<3; i++)
stud[i].display_data(i);
return 0;
}

/*
Write a C++ program to read data of N employee and computer
net salary of each employee
(DA = 52% of Basic and IT = 30% of the gross salary)
*/
#include<iostream.h>
#include<conio.h>
class Employee
{
char emp_name[30];
int emp_number;
float basic, da, it, gross_salary, net_salary;
public:
void read_emp_details(int count){
cout<<"\n\n*** Enter Employee "<<count<<" Details ***";
cout<<"\nEmployee Number: ";
cin>>emp_number;
cout<<"Employee Name: ";
cin>>emp_name;
cout<<"Basic Salary: ";
cin>>basic;
cout<<"\n---- Employee "<<count<<" Datails are saved ----\n\n";
}
float find_net_salary(){
da = basic * 0.52;
gross_salary = basic + da;
it = gross_salary * 0.30;
net_salary = (basic + da) - it;
return net_salary;
}
void display_emp_details(int count){
cout<<"\n\n*** Employee "<<count<<" Details ***\n";
cout<<"\nEmployee Number : "<<emp_number;
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nNet Salary: "<<net_salary;
cout<<"\n--------------------------\n";
}
};
int main(){
Employee emp[100];
int number_of_emp, count;
clrscr();
cout<<"\nPlease enter the number of Employees (Max. 100): ";
cin>>number_of_emp;
for(count=0; count< number_of_emp; count++){
emp[count].read_emp_details(count+1);
}
for(count=0; count < number_of_emp; count++){
emp[count].find_net_salary();
}
for(count=0; count < number_of_emp; count++){
emp[count].display_emp_details(count+1);
}
cout<<"\nPress any key to close!!!";
getch();
return 0;
}

Result

Write a C++ program to use scope resolution operator. Display the various values of
the same variables declared at different scope levels.

Solution
The scope resolution operator is used to access the hidden names which are at
different scope levels. In C++, the scope resolution operator has the following
uses.
? Accessing Global variable
When both global and local variables have the same name then global variable in
hidden inside the local scope. Here, we use the scope resolution operator to refer
to the global variable.

Example
#include <iostream>
using namespace std;

int my_variable = 10; // Global variable my_variable

int main()
{
int my_variable = 100; // Local variable my_variable

cout << "Value of global my_variable is " << ::my_variable << endl;
cout << "Value of local my_variable is " << my_variable << endl;

return 0;
}

You might also like