0% found this document useful (0 votes)
11 views4 pages

C Programs (9-12-2020)

Uploaded by

mohitnajkani786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

C Programs (9-12-2020)

Uploaded by

mohitnajkani786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Programming

Topics Covered= Operator Overloading

Program 209
//209.Example of subscript operator (Non Numeric Subscript)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Course
{
public:
char nm[50];
int id;
void accept()
{
cout<<"\n Enter course Name=";
cin.getline(nm,50);
cout<<"\n Enter course id=";
cin>>id;
cin.sync();
}
void show()
{
cout<<"\n course Name is="<<nm;
cout<<"\n course id="<<id;
}
};
class subject nm= BSc-IT nm= MSc- nm= BCA nm= nm=
{ Id=201 IT Id=601 MCA B.Com
private: Id=501 Id=702 Id=903
Course c[5]; //Containership(Has a Relationship)
public:
void accept()
{
for(int i=0;i<5;i++)
{
c[i].accept();
}
}
void show()
{
for(int i=0;i<5;i++)
c[i].show();
}
int operator [](char nm1[50]) //nm1=”MCA” //nm1=”MA”
{
for(int i=0;i<5;i++) //i=0 to i=4
{

1|Page 9-12-2020
C++ Programming
if(strcmpi(c[i].nm,nm1)==0) //(i=0 “BSc-IT”==”MCA”)
return c[i].id;
}
return -1;
}
};
void main()
{ nm= BSc-IT nm= MSc- nm= BCA nm= nm=
subject p1; Id=201 IT Id=601 MCA B.Com
clrscr(); Id=501 Id=702 Id=903
cout<<endl<<"Enter
Details=\n";
p1.accept();
p1.show();
cin.sync();
char nm1[50];
cout<<"\n Enter name of Course to Search=";
cin.getline(nm1,50); //MCA
cout<<"\n Id of Course is="<<p1[nm1]; //p1[](“MCA”)
getch();
}

/*Output
Enter Details=
Enter course Name=BSc-IT
Enter course id=201
Enter course Name=MSc-IT
Enter course id=501
Enter course Name=BCA
Enter course id=601
Enter course Name=MCA
Enter course id=702
Enter course Name=B.Com
Enter course id=903
Enter name of Course to search=MCA
Id of Course is=702
Enter name of Course to search=MA
Id of Course is=-1
*/
=========================================================

2|Page 9-12-2020
C++ Programming
Program 211
//211.Example of Assigning object to another object
#include<iostream.h>
#include<conio.h>
class Array
{
//Data Members
private:
int size;
int *q;
public:
//Constructor
Array()
{
size=3;
q=new int[size];
}
Array(int sz) //sz=5
{ 10 20 30 40 50 1000(address)
size=sz; 0 1 2
q=new int[size];
1000
} 5 (q)
size 5000
//Member Methods Pointer q is pointing to the
void display() location that has address 1000
{
for(int i=0;i<size;i++) d1 object
cout<<q[i]<<endl;
}
void accept()
{
for(int i=0;i<size;i++)
{
cout<<"Enter Number=";
cin>>q[i];
}
}
};
void main()
{
Array d1(5); //Parameterized Constructor
clrscr();
cout<<endl<<"Enter Details for d1 object=\n";
d1.accept();
cout<<endl<<"Details of d1 object is="<<endl;
3|Page 9-12-2020
C++ Programming
d1.display();
cout<<endl<<"==========================";
Array d2; //Non Parameterized Constructor
d2=d1;
cout<<endl<<"Details of d2 object is=\n";
d2.display();
getch();
}
/*
Output
Enter Details for d1 object=
Enter Number=10
Enter Number=20
Enter Number=30
Enter Number=40
Enter Number=50
Details of d1 object is=
10
20
30
40
50
===========================
Details of d2 object is=
10
20
30
40
50
*/

4|Page 9-12-2020

You might also like