OBJECT ORIENTED LAB MANUAL -1[1] (2)
OBJECT ORIENTED LAB MANUAL -1[1] (2)
PRACTICAL FILE
OF
OBJECT ORIENTED PROGRAMMING LAB
USING C++
(LC-CSE-214G)
Aim:- Write a program that uses a class where the member functions
are defined inside the class.
#include <iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
};
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
OUTPUT
Enter car number : 9999
Aim:- Write a program that uses a class where the member functions defined
outside a class.
#include <iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata(); //function declaration
void showdata();
};
// function definition
void car::getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void car::showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
OUTPUT
#include <iostream>
usingnamespace std;
class Demo
{
private:
staticint X;
public:
staticvoid fun()
{
cout <<"Value of X: " << X << endl;
}
};
//defining
int Demo :: X =10;
int main()
{
Demo X;
X.fun();
return 0;
}
OUTPUT
Value of X: 10
Value of
PRACTICAL-4
Value of X: 10
#include<iostream>
using namespace std;
class Demo {
int val;
public:
Demo(int x = 0) {
val = x;
}
int getValue() const {
return val;
}
};
int main() {
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}
OUTPUT
#include <iostream>
usingnamespace std;
class Demo
{
private:
int A;
int B;
int C;
public:
Demo();
void set(int A, int B, int C);
void print();
};
Demo::Demo()
{
A=1;
B=1;
C=1;
}
void Demo::print()
{
cout<<"Value of A : "<<A<<endl;
cout<<"Value of B : "<<B<<endl;
cout<<"Value of C : "<<C<<endl;
}
int main()
{
Demo obj = Demo(); //Constructor called when object created
obj.print();
obj.set(10,20,30);
obj.print();
return 0;
}
OUTPUT
Value of A : 1
Value of B : 1
Value of C : 1
Value of A : 10
Value of B : 20
Value of C : 30
PRACTICAL-6
#include <iostream>
usingnamespace std;
classABC {
constchar* p;
public:
// default constructor
ABC()
{
void display()
{
cout << p << endl;
}
};
int main()
{
ABC obj;
obj.display();
}
OUTPUT
fair
PRACTICAL-7
#include<iostream.h>
class A
{
int data;
public:
A(int a):data(a)
{
cout<<"A::Construcor...\n";
cout<<"value of data :="<<data<<endl;
};
};
int main()
{
A a1 = 37;
return (0);
}
./a.out
A::Construcor...
value of data :=37
PRACTICAL-8
#include <iostream>
usingnamespace std;
//Class declaration.
class Demo
{
//Private block to declare data member( X,Y ) of integer type.
private:
constint X;
constint Y;
};//End of class
return 0;
}
OUTPUT
Value of d1:
X: 10
Y: 20
Value of d2:
X: 30
Y: 40
PRACTICAL-9
Increment
#include <bits/stdc++.h>
usingnamespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
OUTPUT
Before increment: i = 3
After pre increment: i = 4
DECREMENT
#include <bits/stdc++.h>
usingnamespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator--(int)
{
Integer temp;
temp.i = i--;
return temp;
}
// Driver function
int main()
{
Integer i1(3);
OUTPUT
Before decrement: i = 3
After post decrement: i = 3
PRACTICAL-10
#include<iostream.h>
#include<conio.h>
class complex {
int a, b;
public:
void getvalue() {
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
}
void display() {
cout << a << "+" << b << "i" << "\n";
}
};
void main() {
clrscr();
complex obj1, obj2, result, result1;
obj1.getvalue();
obj2.getvalue();
getch();
}
OUTPUT
#include<iostream>
#include<stdlib.h>
usingnamespace std;
class student
{
string name;
int age;
public:
student()
{
cout<< "Constructor is called\n" ;
}
student(string name, int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size << endl;
void * p = ::operator new(size);
//void * p = malloc(size); will also work fine
return p;
}
void operator delete(void * p)
{
cout<< "Overloading delete operator " << endl;
free(p);
}
};
int main()
{
student * p = new student("Yash", 24);
p->display();
delete p;
OUTPUT
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout<<"Base class content.";
}
};
int main() {
C obj;
obj.display();
return 0;
}
OUTPUT
#include <iostream>
using namespace std;
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
int main() {
Bat b1;
return 0;
}
OUTPUT
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
OUTPUT
Eating bread...
PRACTICAL-15
#include <iostream>
using namespace std;
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
OUTPUT
#include <iostream>
using namespace std;
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
OUTPUT:
#include <iostream>
using namespace std;
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
return 0;
}
OUTPUT:
2+3=5
2.2 + 3.3 = 5.5
PRACTICAL-18
// Class template
template <class T>
class Number {
private:
// Variable of type T
T num;
public:
Number(T n) : num(n) {} // constructor
T getNum() {
return num;
}
};
int main() {
return 0;
}
OUTPUT
int Number = 7
double Number = 7.7