You are on page 1of 5

array and strings }

PROGRAM FOR ARRAY void change(int data)


#include <iostream> {
using namespace std; data = 5;
int main() }
{ OUTPUT:
int numbers[5] = {7, 5, 6, 12, 35}; Value of the data is: 3
cout<< "The numbers are: "; PROGRAM FOR CALL BY REFERENCE:
for (const int &n : numbers) #include<iostream>
{ using namespace std;
cout<< n << " "; void swap(int *x, int *y)
} {
cout<< "\n The numbers are: "; int swap;
for (int i = 0; i < 5; ++i) swap=*x;
{ *x=*y;
cout << numbers[i] << " "; *y=swap;
} }
return 0; int main()
} {
OUTPUT: int x=500, y=100;
The numbers are: 7 5 6 12 35 swap(&x, &y);
The numbers are: 7 5 6 12 35 cout<<"Value of x is: "<<x<<endl;
PROGRAM FOR STRING cout<<"Value of y is: "<<y<<endl;
#include <iostream> return 0;
using namespace std; }
int main() OUTPUT:
{ Value of x is: 100
char str[100]; Value of y is: 500
cout << "Enter a string: "; PROGRAM FOR CALL BY ADDRESS
cin >> str; #include <iostream>
cout << "You entered: " << str << endl; using namespace std;
cout << "\nEnter another string: "; void increment(int *a)
cin >> str; {
cout << "You entered: "<<str<<endl; (*a)++;
return 0; cout << "Value in Function increment: "<< *a <<endl; }
} int main()
OUTPUT: {
Entered a string: Richest person int x = 5;
You entered: Richest increment(&x);
Enter another string: You entered: person cout << "Value in Function increment: "<< x <<endl;
call by value, reference and address. return 0;
PROGRAM FOR CALL BY VALUE: }
#include <iostream> OUTPUT:
using namespace std; Value in Function increment: 6
void change(int data); Value in Function main:6
int main() FUNCTION OVERLOADING:
{ #include <iostream>
int data = 3; using namespace std;
change(data); class Cal {
cout << "Value of the data is: " << data<< endl; public:
return 0; static int add(int a,int b){
return a + b; Room room1;
} room1.length = 42.5;
static int add(int a, int b, int c) room1.breadth = 30.8;
{ room1.height = 19.2;
return a + b + c; cout << "Area of Room = " << room1.calculateArea() <<
} endl;
}; cout << "Volume of Room = " << room1.calculateVolume()
int main(void) { << endl;
Cal C; return 0;
cout<<C.add(10, 20)<<endl; }
cout<<C.add(12, 20, 23); OUTPUT:
return 0; Area of Room = 1309
} Volume of Room = 25132.8
OUTPUT: constructors and destructors.
30 PROGRAM FOR CONSTRUCTOR:
55 #include <iostream>
classes and objects. using namespace std;
PROGRAM FOR CLASSES: class student {
#include <iostream> int rno;
#include <string> char name[10];
using namespace std; double fee;
class MyClass { public:
public: student()
int myNum; {
string myString; }; cout << "Enter the RollNo:";
int main() { cin >> rno;
MyClass myObj; cout << "Enter the Name:";
myObj.myNum = 15; cin >> name;
myObj.myString = "Some text"; cout << "Enter the Fee:";
cout << myObj.myNum << "\n"; cin >> fee;
cout << myObj.myString; }
return 0; void display()
} {
OUTPUT: cout << endl << rno << "\t" << name << "\t" << fee;
15 }
Some text };
PROGRAM FOR OBJECTS: int main()
#include <iostream> {
using namespace std; student s;
class Room { s.display();
public: return 0;
double length; }
double breadth; OUTPUT:
double height; Enter the RollNo: 029
double calculate Area() { Enter the Name: DEEPAK K
return length * breadth; Enter the Fee: 500
} 029 DEEPAK K 500
double calculate Volume() { PROGRAM FOR DESTRUCTORS:
return length * breadth * height; } #include<iostream>
}; using namespace std;
int main() { class Test
{ friend int addFive(Distance);
public: public:
Test() Distance() : meter(0) {}
{ };
cout<<"\n Constructor executed"; int addFive(Distance d) {
} d.meter += 5;
Test() return d.meter;
{ }
cout<<"\n Destructor executed"; int main() {
} Distance D;
}; cout << "Distance: " << addFive(D);
main() return 0;
{ }
Test t,t1,t2,t3; OUTPUT:
return 0; Distance:5
} PROGRAM FOR FRIEND CLASS:
OUTPUT: #include <iostream>
Constructor executed using namespace std;
Constructor executed class ClassB;
Constructor executed class ClassA {
Constructor executed private:
Destructor executed int numA;
Destructor executed friend class ClassB;
Destructor executed public:
Destructor executed ClassA() : numA(12) {}
PROGRAM FOR VARIOUS FORMS OF };
INHERITANCE: class ClassB {
#include <iostream>
private:
using namespace std;
class Account int numB;
{ public:
public: ClassB() : numB(1) {}
float salary = 60000; int add() {
};
class Programmer: public Account ClassA objectA;
{ return objectA.numA + numB;
public: }
float bonus = 5000; };
};
int main() {
int main(void)
{ ClassB objectB;
Programmer p1; cout << "Sum: " << objectB.add();
cout<<"Salary: "<<p1.salary<<endl; return 0;
cout<<"Bonus: "<<p1.bonus<<endl; }
return 0; }
OUTPUT: OUTPUT:
Salary: 60000 Sum: 13
Bonus: 5000 UNARY OPERATOR OVERLOADING:
Friend function and class: #include<iostream.h>
PROGRAM FOR FRIEND FUNCTION: #include<conio.h>
#include <iostream> class complex {
using namespace std; int a, b, c;
class Distance { public:
private: complex() {
int meter; }
void getvalue() { Complex_num operator - (Complex_num obj)
cout << "Enter the Two Numbers:"; {
cin >> a>>b; Complex_num A;
} A.x = x - obj.x;
void operator++() { A.y = y - obj.y;
a = ++a; return (A);
b = ++b; }
} void print()
void operator--(){ {
a = --a; cout << x << " + " << y << "i" << "\n";
b = --b; }
} void print2()
void display() { {
cout << a << "+\t" << b << "i" << endl; cout << x << " - " << y << "i" << "\n";
} }
}; };
void main() { int main ()
clrscr(); {
complex obj; Complex_num x1, y1, sum, sub;
obj.getvalue(); x1.inp();
obj++; y1.inp();
cout << "Increment Complex Number\n"; sum = x1 + y1;
obj.display(); sub = x1 - y1
obj--; cout << "\n Entered values are: \n";
cout << "Decrement Complex Number\n"; cout << " \t";
obj.display(); x1.print();
getch(); cout << " \t";
} y1.print();
OUTPUT: cout << "\n The addition of two complex (real and
Enter the two numbers: 3 6 imaginary) numbers: ";
Increment Complex Number 4 + 7i sum.print();
Decrement Complex Number 3 + 6i cout << "\n The subtraction of two complex (real and
BINARY OPERATOR OVERLOADING: imaginary) numbers: ";
#include <iostream> sub.print2();
using namespace std; return 0;
class Complex_num }
{ OUTPUT:
int x, y; Input two complex number:
public: 4+i5
void inp() Input two complex number:
{ Entered values are:
cout << " Input two complex number: " << endl; 4 + 0i
cin >> x >> y; 0 + 0i
} The addition of two complex (real and imaginary)
Complex_num operator + (Complex_num obj) numbers: 4 + 0i
{ The subtraction of two complex (real and imaginary)
Complex_num A; numbers: 4 - 0i
A.x = x + obj.x; FUNCTION TEMPLATES:
A.y = y + obj.y; #include <iostream>
return (A); using namespace std;
} template <typename T>
T add(T num1, T num2) { Derived derived1;
return (num1 + num2); Base* base1 = &derived1;
} base1->print();
int main() { return 0;
int result1; }
double result2; OUTPUT:
result1 = add<int>(2, 3); Derived
cout << "2 + 3 = " << result1 << endl; ABSTRACT CLASS:
result2 = add<double>(2.2, 3.3); #include <iostream>
cout << "2.2 + 3.3 = " << result2 << endl; using namespace std;
return 0; } class Abstract
OUTPUT: {
2+3=5 int i, j;
2.2 + 3.3 = 5.5 public:
PROGRAMS FOR THE CLASS TEMPLATES: virtual void setData(int i = 0, int j = 0) = 0;
#include <iostream> virtual void printData() = 0; };
using namespace std; class Derived : public Abstract
template <class T> {
class Number { int i, j;
private: public:
T num; Derived(int ii = 0, int jj = 0) : i(ii), j(jj)
public: {
Number(T n) : num(n) {} // constructor cout << "Creating object " << endl;
T getNum() { }
return num; void setData(int ii = 0, int jj = 0)
} }; {
int main() { i = ii;
Number<int> numberInt(7); j = jj;
Number<double> numberDouble(7.7); }
cout << "int Number = " << numberInt.getNum() << endl; void printData()
cout << "double Number = " << numberDouble.getNum() {
<< endl; cout << "Derived: :i = " << i << endl
return 0; << "Derived: :j = " << j << endl;
} } };
OUTPUT: int main()
int Number = 7 {
double Number = 7.7 Derived d;
ILLUSTRATE VIRTUAL FUNCTION: cout << "Current data " << endl;
#include <iostream> d.printData();
using namespace std; d.setData(10, 20);
class Base { cout << "New data " << endl;
public: d.printData();
virtual void print() { }
cout << "Base Function" << endl;} OUTPUT:
}; Creating object
class Derived : public Base { Current data
public: Derived: :i = 0
void print() { Derived: :j = 0
cout << "Derived Function" << endl; New data
} }; Derived: :i = 10
int main() { Derived: :j = 20

You might also like