Start and stop are default for all programs
1a) Static data member
Steps:
1. Declare a class as item
2. Declare the static member fn. Count and an integer variable number.
3. In public section create the integer as a number and assign the value for number=a and
increment the count.
4. Declare the fn. Getcount and print the count value.
5. Then initialize the static fn. as int item::count.
6. Initialize the 3 integer variable and display output.
(fn.=function).
1b) static member fn.
[Link] the class as item
[Link] the static member fn. count and an integer code.
[Link] public,initialize the fn. setcode and increment count.
[Link] fn. showcode to print code.
[Link] initialize the static fn. int test :: count.
[Link] 3 variables in main and display output.
1c) Default arguments
1.A fn. printline is declared with default arguments.
[Link] fn. printline is defined so as to print character.
[Link] fn. printline is called inside main
3.1 with less number of arguments compared to the printline prototype to access
the default arguments.
3.2 with equal number of arguments compared to the printline prototype to
override the default arguments
1d) Function overloading
1. Define a function with overloading using same function name but with different number
and type of arguments.
1.1 The fn. Show() is overloaded with integer ,float and character datatype.
[Link] fn. Show() is defined to print the output for different datatype.
[Link] the main the fn.
The show() is called with different type of arguments to invoke the overloaded functions.
2a) Friend function
[Link] class name as xyz.
[Link] an integer variable say x,as [Link] public define a fn. and assign x=i.
[Link] friend fn. as max with xyz and abc as arguments.
[Link] another class with name abc in that declare a private variable as ‘a’. In public declare a
[Link] and assign a=i.
[Link] max fn use if condition as (m.x>=n.a) then print x,else print a.
[Link] main fn. create an object for abc then access setvalue fn. similarly,create another object for
xyz fn. and access the setvalue fn.
Then print which is maximum.
2b) / returning obj.
[Link] the class name as complex.
2. Declare float variable say x and y in private.
3. In public, create a fn. Input() with arguments float real,float img.
Assign x=real and y=img.
4. friend fn. is created as sum() to calculate the sum.
4. In fn. void show display x and y in that performs some tasks like c3.x=c1.x+c2.x and
c3.y=c1.y+c2.y and return (c3).
[Link] main, declare objects A,B and C. This objects is used to access the member fn. and
variable in class .
2c) Call by value and address
[Link] call by value fn with argument as int a, increment a.
[Link] call by address fnwith argument as pointer variable,increment a.
[Link], in call by reference using address operator (int&a),increment a.
[Link] main,
Call the functions with different arguments passing methods and display the results.
3a) Array of objects
[Link] the class name as student.
[Link] private,initialize character and integer datatypes.
[Link] public,fn. get and display is declared.
[Link] the class ,initialize the static fn as void student :: get(),in that get roll
no. and name.
[Link] another ststic member fn. as void student ::display, in that print roll no an
Name.
[Link] main, assign int I and an object s[2] for the class [Link] for loop for accessing
member fn.
3b) Pointer to object
[Link] class name as student.
[Link] public, initialize the variable roll no and name.
[Link] print fn. print roll no and name.
4. In main,declare pointer objects say s1,s2 for student. Assign s1=newstudent
s2=newstdent(mry space allocation)
[Link] (*s1).rno=1 and using strcpyfn access the name. Similarly, for s2 and print both.
3c) Local classes
[Link] main, initialize [Link] as an integer and get input from user.
declare a fn. test and pass rno as argument - test(rno).
[Link] test [Link] is declared as student. In public,initialize rollno and name.
[Link] get fn, get name and and assign rollno and rno received from main.
4. print() [Link] defined to print the values
5. create an object for the class locally within the function and invoke the get and print functions
to view the results.
4a)Nested class public
[Link] the class name as out with public variable y.
2. In public of class outside, another class name inside is declared in public with integer varianle
x and andfn.get1 and display ().
3. In get function get x from user and in display fn. print that integer x.
4. create an object for the inside class int1 within the public section of Outside class.
5. define get() for outside class to receive value for y from user.
6. define function add() to add the public variable fo both the classes.
4. In add [Link] another variable c, so as to perform c=int1.x+y then print c.
5. In main, the outside class object and inside class objects are called to access the functions and
display the results.
4b)Nested class private
1. Declare class name as add1 and in the private of add1 another class add2 is declared.
2. In public of add2, initialize integer (a) and a fn. get1 to receive the value of a.
3. In public, of add1 , initialize variable b create an object for add2 say s1.
4. In public, of add1 initialize a function to receive the value of b
5. define another function display () to create the object for class add2 and calculate the sum of
the two class variables and display results.
5. In main, create object for class add1 and invoke the display() to calculate the sum and display
the results.
#include<iostream.h>
#include<conio.h>
#include<string.h>
Class
Complex
{
Float real;
Float img;
Public:
Complex(float tempreal=0,float tempimg=0)
{
Real=tempreal;
Img=tempimg;
}
Complex add(complex comp2)
{
Float tempreal;
Float tempimg;
Tempreal=real+[Link];
Tempimg=img+[Link];
Return complex(temp real,tempimg);
}
Complex operator +(complex comp2)
{
Float tempreal;
Float tempimg;
Tempreal=real+[Link];
Tempimg=img+[Link];
Return complex(temp real,tempimg);
}
Void display()
{
Cout<<real<<”i”<<img<<”\n”;
}
};
Void main()
{
Complex comp1(10,20);
Complex comp2(20,30);
Complex compresult1,compresult2;
Compresult1=[Link](comp2);
[Link]();
Compresult2=comp1+comp2;
[Link]();
Getch();
}
#include<iostream.h>
#include<conio.h>
class distance
{
intfeet,inch;
public:
distance()
{
cout<<"\n default constructor";
}
distance(intf,int i)
{
cout<<"\n parametersized constructor \n";
feet=f;
inch=i;
}
distance(const distance &dd)
{
cout<<"copy constructor \n";
feet=[Link];
inch=[Link];
}
void operator=(const distance &d)
{
cout<<"overload assign operator \n";
feet=[Link];
inch=[Link];
}
display()
{
cout<<feet<<"feet"<<inch<<"inch"<<"\n";
}
};
void main()
{
clrscr();
distance d1,d4;
distance d2(10,10);
distance d3(d2);
d1=d2;
[Link]();
[Link]();
[Link]();
[Link]();
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
private:
introllno;
char name[10];
char address[10];
public:
friendostream&operator<<(ostream&,student&);
friendistream&operator>>(istream&,student&);
};
ostream&operator<<(ostream&tempout,student&tempstudent)
{
tempout<<"rollno is"<<[Link]<<"\n";
tempout<<"address is"<<[Link]<<"\n";
returntempout;
}
istream&operator>>(istream&tempin,student&tempstudent)
{
cout<<"enter rollno:";
tempin>>[Link];
cout<<"\n";
cout<<"enter name:";
tempin>>[Link];
cout<<"\n";
cout<<"enter address:";
tempin>>[Link];
cout<<"\n";
returntempin;
}
void main()
{
clrscr();
studentcaptainstudent;
cin>>captainstudent;
cout<<"following is captainstudent data\n"<<captainstudent<<"\n
bye\n";
getch();
}
#include<stdlib.h>
#include<iostream.h>
#include<malloc.h>
#include<conio.h>
class test
{
int i;
public:
test():i(0)
{
cout<<"contructor is called\n";
};
~test()
{
cout<<"destructor is called\n";
};
void *operator new(size_t size);
void operator delete(void *p);
};
void *test::operator new(size_t size)
{
void *p=malloc(size);
if(!p)
{
cout<<"memory allocate failure";
exit(0);
}
return p;
}
void test::operator delete(void *p)
{
free(p);
}
int main()
{
test *testptr=new test;
delete(testptr);
getch();
}
#include<iostream.h>
#include<conio.h>
class complex
{
floatx,y;
public:
complex()
{
x=0;
y=0;
}
complex(float real,floatimg)
{
x=real;
y=img;
}
void show()
{
cout<<x<<"+j"<<y<<endl;
}
friend complex operator+(complex,complex);
};
complex operator +(complex obj1,complex obj2)
{
complex temp;
temp.x=obj1.x+obj2.x;
temp.y=obj1.y+obj2.y;
return temp;
}
int main()
{
clrscr();
complex c1(1.6,3.6);
complex c2(2.6,4.9);
complex c3;
c3=c1+c2;
[Link]();
[Link]();
[Link]();
return 0;
getch();
}
#include<iostream.h>
#include<conio.h>
class complex
{
inta,b;
public:
complex()
voidgete()
{
cout<<"enter two no:";
cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator--()
{
a=--a;
a=--b;
}
void display()
{
cout<<a<<"+i"<<b<<""<<endl;
}
};
void main()
{
complexobj;
[Link]();
obj++;
cout<<"increasment complex no\n";
[Link]();
obj--;
cout<<"decrement complex no\n";
[Link]();
getch();
}
#include<iostream.h>
class matrix
{
int Element[3][3];
public:
matrix(){};
matrix(intTempmatrix[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
Element[i][j]=Tempmatrix[i][j];
}
void read()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>Element[i][j];
}
void display()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<Element[i][j]<<"";
}
cout<<"\n";
}
}
friend matrix operator *(matrix,int);
friend matrix operator *(int,matrix);
};
matrix operator *(matrix Tempmatrix,int multiplier)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
[Link][i][j]=multiplier * [Link][i][j];
return matrix([Link]);
}
matrix operator *(intmultiplier,matrixTempmatrix)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
[Link][i][j]=multiplier * [Link][i][j];
return matrix([Link]);
}
void main()
{
int arrayofint1[][3]={1,2,3,4,5,6,7,8,9};
int arrayofint2[][3]={4,5,6,7,8,9,1,2,3};
matrix m1(arrayofint1);
matrix m2(arrayofint2);
matrix m3,m4;
[Link]();
m3=m1*5;
[Link]();
[Link]();
m4=5*m2;
[Link]();
}
class Date
{
private:
int m_nMonth;
int m_nDay;
int m_nYear;
public:
void SetDate(int nMonth, int nDay, int nYear)
{
m_nMonth = nMonth;
m_nDay = nDay;
m_nYear = nYear;
}
int GetMonth() const { return m_nMonth; }
int GetDay() const { return m_nDay; }
int GetYear() const { return m_nYear; }
};
void PrintDate(const Date cDate)
{
// although cDate is const, we can call const member functions
cout << [Link]() << "/" <<[Link]() << "/" <<
[Link]() <<endl;
}
int main()
{
const Date cDate;
[Link](10, 16, 2020);
PrintDate(cDate);
return 0;
}
#include <iostream>
using namespace std;
class Object
{
public:
Object() {}
void Print() const
{
cout << "const" << endl;
}
void Print()
{
cout << "mutable" << endl;
}
};
void print_obj(const Object& obj)
{
[Link]();
}
int main()
{
Object obj1;
const Object obj2;
Object*const pobj1 = &obj1;
print_obj(obj1);
print_obj(obj2);
[Link]();
[Link]();
pobj1->Print();
return 0;
}
Output
const
const
mutable
const
mutable
// Returning objects
1. Declare class name as complex.
[Link] float variable (say x,y) in private.
[Link] public,create a fn. input with arguments float real and float img.
[Link] x=real & y=img also. Frnd fn. is created with sum as name.
[Link] fn. show,display x and y and also perform some fn like c3.x=c1.x+c2.x and
c3.y=c1.y+c2.y and return c3.
[Link] main ,declare objects A,B,C. This objects is used to access the member
function in class.
//Call by value and address
[Link] call by value fn with argument as int a, increment a.
[Link] call by address fnwith argument as pointer variable,increment a.
[Link], in call by reference (int&a),increment a.
[Link] main,display the results.
//Array of objects
[Link] the class name as student.
[Link] private,initialize character and integer datatypes.
[Link] public,fn. get and display is declared.
[Link] the class ,initialize the static fn as void student :: get(),in that get roll
no. and name.
[Link] another ststic member fn. as void student ::display, in that print roll no an
Name.
[Link] main, assign int I and an object s[2] for the class [Link] for loop for
accessing member fn.
// Pointer to object
[Link] class name as student.
[Link] public, initialize the variable roll no and name.
[Link] print fn. print roll no and name.
4. In main,declare pointer objects say s1,s2 for student. Assign s1=newstudent
s2=newstdent(mry space allocation)
[Link] (*s1).rno=1 and using strcpyfn access the name. Similarly, for s2 and
print both.
// Local classes
[Link] main, initialize [Link] as an integer, and declare a fn. test with int as argument
then test(rno).
[Link] test [Link] is declared as student. In public,initialize [Link] and name.
[Link] get fn, get name and roll no. In print [Link] both.
4.s1 is an object for class then access it.
//Nested class public
[Link] the class name as out.
2. In public of class outside, another class is declared in public of which declare int
x and andfn.get1 and x value.
3. In display fn. print that integer. Outside class inside initialize the int y and an
object in1 to access inside class.
4. In fn get 2, get y value. In add [Link] another variable c, so as to perform
c=int1.x+y then print c.
5. In main, access the private data’s and fn.
//Nested class private
1. Declare class name as add1 and in the private of add1 another class add2 is
declared.
2. In public, initialize integer and a fn. get1 the value of a. Then close this class.
[Link] public, of add1 , initialize int b create an object for add2 say s1.
[Link] fn. get2, get b value. In display fn. c, where c=s1.a+b.
5. In main, display the results.