Oops Code
Oops Code
int main()
{
Student s1; //creating an object of
Student
Student s2; //creating an object of
Student
s1.insert(201, "Sonoo");
s2.insert(202, "Nakul");
s1.display();
s2.display();
return 0;
}
Class Activity1:
1. Nihal is trying to calculate the area
and volume of the rectangular room
help Nihal to write a cpp program to
calculate the roomarea and volume
with the help of valid data members
and member functions.
Program:
// Program to illustrate the working of
// objects and class in C++ Programming
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
return 0;
}
Assignment questions
1. 1. C++ program to read time in 2. Define a class to represent a bank account.
HH:MM:SS format and convert into Include the following members:
total seconds using class. Data members
1. Name of the depositor
2. Account number
3. Type of account
4. Balance amount in the
account
Member functions
1. To assign initial values
2. To deposit an amount
3. To withdraw an amount
after checking the balance
4. To display name and
balance
Write a main program to test the
program.
cout << "Local variable: " << c << "\ void Game::play()
n"; {
cout << "Global variable: " << ::c << cout << "Function defined outside the
"\n"; // Using scope resolution operator class.\n";
}
return 0;
} int main()
{
Game g;
g.play();
return 0;
}
3. // C++ program to 4. C++ program to demonstrate
demonstrate public private
// access modifier // access modifier
#include<iostream> #include<iostream>
using namespace std; using namespace std;
obj1.setId(81);
obj1.displayId();
return 0;
}
Class Activity
1. Wrt a cpp program to create a class KLETECH employee with private data member
as a salary and public member functions as getsalary and setsalary. Read the salary at
compile time and print.
#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;
}
3.copy 4. Destructors
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class A class Employee
{ {
public: public:
int x; Employee()
A(int a) // parameterized const {
ructor. cout<<"Constructor Invoked
{ "<<endl;
x=a; }
} ~Employee()
A(A &i) // copy constructor {
{ cout<<"Destructor Invoked"
x = i.x; <<endl;
} }
}; };
int main() int main(void)
{ {
A a1(20); // Calling the paramete Employee e1; //creating an object
rized constructor. of Employee
A a2(a1); // Calling the copy con Employee e2; //creating an object
structor. of Employee
cout<<a2.x; return 0;
return 0; }
}
int main()
{
set A;
A.input();
A.display();
return 0;
}
Class Activity
Static data members
#include <iostream>
#include<string.h>
void getdata() {
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}
void putdata() {
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl;
}
};
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();
Student s2;
s2.getdata();
s2.putdata();
Student s3;
s3.getdata();
s3.putdata();
cout << "Total objects created = " << Student::objectCount << endl;
return 0;
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
int main() {
Distance D;
cout << "Distance: " <<
addFive(D);
return 0;
}
Output:
// C++ program to demonstrate the A::a=0
working of friend class
#include <iostream>
class A {
private:
int a;
public:
A() { a = 0; }
friend class B; // Friend Class
};
class B {
private:
int b;
public:
void showA(A& x)
{
// Since B is friend of
A, it can access
// private members of
A
std::cout << "A::a="
<< x.a;
}
};
int main()
{
A a;
B b;
b.showA(a);
return 0;
}
// single inheritance Output
public:
int x;
void getdata()
};
private:
int y;
public:
void readdata()
void product()
};
int main()
a.getdata();
a.readdata();
a.product();
return 0;
} //end of program
Gender: Male
using namespace std; Age: 23
{ Level: Bachelors
private:
Display data
char fname[100],lname[100],gender[10];
First Name : Harry
protected:
Last Name : Potter
int age;
Gender : Male
public:
Age : 23
void input_person();
College : Abc International College
};
private:
char college_name[100];
char level[20];
public:
void input_student();
void display_student();
};
void person::input_person()
cin>>fname;
cin>>lname;
cout<<"Gender: ";
cin>>gender;
cout<<"Age: ";
cin>>age;
void person::display_person()
cout<<"Gender : "<<gender<<endl;
cout<<"Age : "<<age<<endl;
void student::input_student()
person::input_person();
cout<<"College: ";
fflush(stdin);
gets(college_name);
cout<<"Level: ";
cin>>level;
void student::display_student()
person::display_person();
cout<<"College :
"<<college_name<<endl;
cout<<"Level : "<<level<<endl;
int main()
student s;
cout<<"Input data"<<endl;
s.input_student();
cout<<endl<<"Display data"<<endl;
s.display_student();
getch();
return 0;
#include<conio.h> Code:13
{ Name:Roger Taylor
private: Code:13
int code;
public:
void getdata();
void display();
};
private:
int speed;
public:
void getdata();
void display();
};
void staff::getdata()
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
void staff::display()
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
void typist::getdata()
cout<<"Speed:";
cin>>speed;
void typist::display()
cout<<"Speed:"<<speed<<endl;
int main()
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
getch();
return 0;
}
// multiple inheritance Output: enter value of x:10
enter value of y:10
using namespace std; Sum =20
class A
public:
int x;
void getx()
};
class B
public:
int y;
void gety()
};
class C : public A, public B //C is derived
from class A and class B
public:
void sum()
};
int main()
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
} //end of program
class A
public:
void display()
};
class B
public:
void display()
};
class C: public A, public B
public:
};
int main()
C sample;
getch();
return 0;
float specific_gravity;
public:
void input()
cin>>specific_gravity;
void output()
cout<<"Specific gravity:
"<<specific_gravity<<endl;
};
class fuel
{
float rate;
public:
void input()
cin>>rate;
void output()
cout<<"Rate(per liter):
$"<<rate<<endl;
};
public:
void input()
{
liquid::input();
fuel::input();
void output()
liquid::output();
fuel::output();
};
int main()
petrol p;
cout<<"Enter data"<<endl;
p.input();
cout<<endl<<"Displaying data"<<endl;
p.output();
getch();
return 0;
public: 2
int x, y; 3
};
{
public:
void product()
};
public:
void sum()
};
int main()
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
} //end of program
Enter data
#include <iostream>
Age: 21
Gender: Male
using namespace std;
Name of College/School: Abc Academy
Level: Bachelor
class person
{
Displaying data
char name[100],gender[10];
Name: John Wright
int age;
Age: 21
public:
Gender: Male
void getdata()
{ Name of College/School: Abc Academy
Age: 24
cin>>age;
Gender: Female
cout<<"Gender: ";
Salary: $29000
}
void display()
Displaying data
{
void getdata()
person::getdata();
fflush(stdin);
gets(institute);
cout<<"Level: ";
cin>>level;
void display()
person::display();
cout<<"Name of College/School:
"<<institute<<endl;
cout<<"Level: "<<level<<endl;
};
char company[100];
float salary;
public:
void getdata()
person::getdata();
fflush(stdin);
gets(company);
cout<<"Salary: Rs.";
cin>>salary;
void display()
person::display();
cout<<"Name of Company:
"<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};
int main()
student s;
employee e;
cout<<"Student"<<endl;
cout<<"Enter data"<<endl;
s.getdata();
cout<<endl<<"Displaying data"<<endl;
s.display();
cout<<endl<<"Employee"<<endl;
cout<<"Enter data"<<endl;
e.getdata();
cout<<endl<<"Displaying data"<<endl;
e.display();
getch();
return 0;
// multilevel inheritance Output
Enter value of y= 3
class base //single base class
{
Enter value of z= 3
public:
int x;
Product= 18
void getdata()
};
public:
int y;
void readdata()
};
private:
int z;
public:
void indata()
void product()
};
int main()
{
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
} //end of program
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
void display()
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
};
char company[100];
float salary;
public:
void getdata()
person::getdata();
fflush(stdin);
gets(company);
cout<<"Salary: Rs.";
cin>>salary;
void display()
person::display();
cout<<"Name of Company:
"<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};
int number;
public:
void getdata()
employee::getdata();
cout<<"Number of programming
language known: ";
cin>>number;
void display()
employee::display();
cout<<"Number of programming
language known: "<<number;
};
int main()
programmer p;
cout<<"Enter data"<<endl;
p.getdata();
cout<<endl<<"Displaying data"<<endl;
p.display();
getch();
return 0;
class A
public:
int x;
};
class B : public A
{
public:
x = 10;
};
class C
public:
int y;
y = 4;
};
{
public:
void sum()
};
int main()
obj1.sum();
return 0;
} //end of program
Hybrid inheritance
#include<conio.h
class stu
int id;
char name[20];
void getstu(){
};
public:
void getmarks(){
};
protected:
int spmarks;
public:
void getsports(){
};
int tot;
float avg;
public :
void show(){}
tot=m+p+c;
avg=tot/3.0;
};
void main(){
result r;//object//
r.getstu();
r.getmarks();
r.getsports();
r.show();
getch();
};
}
void show_y(void)
{
cout<<"y="<<y;
}
};
cout<<"m="<<m;
cout<<"n="<<n;
}
};
int main()
{
gamma g(5,10.75,20,30);
g.show_x();
g.show_y();
g.show_mn();
return 0;
}
Base and derived class constrctors OUTPUT:
Base default constructor
Base default constructor
2. Base class Default Constructor in Derived default constructor
Derived class Constructors Base default constructor
Derived parameterized constructor
class Base
int x;
public:
// default constructor
Base()
};
int y;
public:
// default constructor
Derived()
// parameterized constructor
Derived(int i)
};
int main()
Base b;
Derived d1;
Derived d2(10);
int x;
public:
// parameterized constructor
Base(int i)
x = i;
};
int y;
public:
// parameterized constructor
Derived(int j):Base(j)
y = j;
}
};
int main()
Derived d(10) ;
int main()
{
ClassD obj;
//Statement 1
obj.a = 100;
//Statement 2
obj.b = 20;
obj.c = 30;
obj.d = 40;
obj.getfun();
cout<< "\n A : "<< obj.a;
cout<< "\n B : "<<
obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<<
obj.d;