Professional Documents
Culture Documents
AT
MANI SHANKAR
II Year DIPLOMA (III Semester)
ELECTRONICS & COMMUNICATION
PREFACE
Practical training is an important part of the engineering. During
practical training, we learn how work is actually done, in the real
sense. Beside this we also learn code of conduct as well as how
to behave in the working environment. After getting training we
get accustom to the working environment. These things are
important part and we learn to apply them in our professional
life.
In today information computer are being used in every
occupation. They are being used by the people of all age and
profession in there work, As well as in there leisure. This new
social revolution has change the basic concept of computing.
Computing, in today’s information age is no more limited to
computer programmers and computer engineer it has become
and activity of common man. so one should know how to
program a computer.
CONTENTS
PREFACE
ACKNOWLEDGEMENT
INTRODUCTION 1
STEPS IN RUNNING A C++ PROGRAM 3
DISADVANTAGES 4
CONSTANT,VARIABLES AND DATA TYPES 5
OPERATORS ANDEXPRESSIONS 8
DATA I/P AND O/P 10
CONTROL FLOW STATEMENTS 11
FUNCTIONS 16
ARRAY 19
STRUCTURE 20
UNION 20
OOPS 21
CHARACTERISTICS OF LOOPS 24
CONSTRUCTORS AND DESTRUCTORS 25
FRIEND FUNCTIONS 26
OPERATOR OVERLOADING 26
INHERITANCE 28
FILE HANDLING 35
APPLICATIONS 39
COADING OF PROJECT 40
Introduction to programming languages(C++)
Disadvantages of ‘C/C++’:
C++ keywords
Data types
The important data types used in C++ are:
Char:
It holds individual characters and occupies 1 byte.
Int:
It requires 2 bytes and range of storage is -32767 to +32768, it
stores only the whole integer numbers.
Float:
It can hold decimal numbers also; it takes 4 bytes.It can give up to
6 digits after decimal.
Double:
It also stores the decimal values but it can give up to 15 digits after
decimal. It requires 8 bytes.
We can also add some modifiers in front of these data types so that
we can alter their range according to our need. Important modifiers
are short, long, signed, unsigned.
Constants
Like identifiers or variables we can have constants of the type int,
float, and char.
Integer constants:
They are simply the integer valued numbers. They are sequence of
digits taken between 0 and 9. Zero cannot be the starting digit.
They have a positive or a negative sign in their front. They can be
represented as decimal, octal and hexadecimal.
Floating point constant:
A floating point constant is a decimal number that contains either
decimal or an exponent form. It is a sequence of digits having a
decimal in between. It can also have a positive or a negative sign in
front. Its general exponential form is: mantissa E exponent.
Character constant:
It is simply a single character. It is always defined inside single
quotation (‘a’). We can also write ‘4’ but it is different from 4
because in this case ‘4’ is represented by it’s ASCII(American
standard code for information interchange) value which is different
for every character being used in c.
String constant:
A string is simply a sequence of characters. It is represented by
putting double quotation (“welcome”). The compiler puts a null
(‘\0’) character at the end of every string.
The chart below shows the operators with the order of precedence:
Operator’s name Operators’
1. Unary operator - ++ -- !
2. Arithmetic multiply */%
divide, modulus
3. Arithmetic add, + -
Subtract
4. Relational operator <= < > >=
5. Equality operators == !=
6. Logical and &&
7. Logical OR ||
8. Conditional operator ?:
9. Assignment operator = += -= *= /= %=
4. do-while loop: Its purpose is just the same as that of while loop,
but the functioning is entirely different from while loop.
Syntax: do
Statement;
While (expression);
The difference between while and do-while is that the do-while
loop works once even if the condition becomes false in the
beginning.
e.g. #include<stdio.h>
void main()
{
int i= 1, sum;
Sum=0;
do
sum = sum + i;
while (i<10);}
Here before checking of condition takes place the compiler
already adds 1 to sum, hence the result will be different from the
for loop addition.
Functions
1. Library functions.
2. User defined functions.
Recursive functions
Arrays
Unions
Unions is just same as the structure definition, this is also used to
group together same variables.
The difference between structure and union is that in union all the
variables are given same memory location whereas in structure
each variable is given separate memory, therefore the union is used
to save the wastage of memory.
storage-class union tag-name
{
data-type member1;
data-type member2;
………………….
data-type membern;
} variable1, variable2… variablen;
OOPS
Both the programming languages c & c++ are related to each other.
In real sense we can directly say that c++ language is extension
(increment) of c language by observing the “++” operator which
tells the incrementation by one in c language.
All the above written topics are used in nearly the same way as in C
language.
C++ contains the new concept of OOPS (object oriented
programming), rest of the conceptual portion of c++ is same as that
of c language. The small difference between them is of keywords
e.g. c++ uses cout and cin as their input and output functions
whereas c uses printf and scanf on the same place.
OOPS concept
Class.
A class description consists of two parts:
(a) class definition of the components (data members and member
functions).
(b) class implementation or definition of member functions.
e.g. class person
{
private:
char name[20];
int age;
double height;
public:
void getdata(void);
void setdata(char *string, int a, double h);
void showdata(void);
};
The data members and member functions are defined in three types
of accessibility options.
1. private: The private data members and member functions are
accessible only within the class description. They are hidden
from rest of program, therefore it comes under the concept of
data hiding.
2. public: Those which can be used outside of the class definition.
They act as a route between private members and rest of the
program.
3. protected: It is helpful in inheritance, any member declared as
protected is accessible by the member function within its class
and any immediately derived class, cannot be accessed by
outside function of these two classes.
Destructor function
When an object is created it calls the constructor function.
whenever a program exits all its objects are expired, during this
process the program calls a function called destructor function.
The destructor function helps undo the work of the constructor. i.e.
if constructor uses new operator then the destructor uses delete
operator.
The destructor has the same name as that of class followed by a
tilde(~) sign. It doesnot have any return type. e.g. ~person(void).
Operator overloading
Function overloading
Inheritance
The formation of derived classes out of the base class and using the
visibility options according to our requirement is known as
inheritance.
The definition of derived class is given by:
Class derive
d::public/private/protected base
{
public:
void display(void);
};
The way in which a derived class is being called decide how the
variables get the accessibility. To know the accessibility we use the
chart below:
Base Public Private Protected
class derivation derivation derivation
visibility
Private Not Not Not
inherited inherited inherited
Protected Protected Private Protected
Public Public Private Protected
Types of inheritance:
Class father
{
private:
int age;
char name[20];
public:
void get();
void show();
};
class mother: public father
{
private:
int age;
char name[20];
public:
void get();
void show();
};
2. Multilevel inheritance: The mechanism of deriving a class
from another derived class.
Class father
{
private:
int age;
char name[20];
public:
void get();
void show();
};
class mother: public father
{
private:
int age;
char name[20];
public:
void get();
void show();
};
class daughter:public mother
{
private:
int std;
char name[20];
public:
void get();
void show();
};
Class father
{
private:
int age;
char name[20];
public:
void get();
void show();
};
class mother
{
private:
int age;
char name[20];
public:
void get();
void show();
};
class daughter:public father,public mother
{
private:
int std;
char name[20];
public:
void get();
void show();
};
Class father
{
private:
int age;
char name[20];
public:
void get();
void show();
};
class daughter:public father
{
private:
int std;
char name[20];
public:
void get();
void show();
};
class son:public father
{
private:
int rollno;
char name[20];
public:
void get();
void show();
};
public:
void gets();
void shows();
};
class result:public test, public sports
{
float total;
public:
void display();
};
File handling in C++
Header files
ifstream, ofstream, and fstream classes are declared in the fstream.h
file. This file also includes the iostream.h header file.
String input/output
Write:
#include<stdio.h>
void main()
{
ofstream outfile(“test.txt”);
outfile<<”hello how are you\n”;
outfile<<”welcome\n”;
}
Read:
void main()
{
const int max=80;
char buffer [max];
ifstream infile(“test.txt”);
while(infile)
{
infile.getline(buffer, max);
cout<<buffer;
}
}
Read:
#include<fstream.h>
class person
{
private:
char name[20];
int age;
public:
void showdata()
{
cout<<”name”<<name;
cout<<”age”<<age;
}
};
void main()
{
person p;
ifstream infile(“person.dat”);
infile.read((char*)&p;sizeof(p));
p.showdata();
}
Applications of ‘C/C++’:
• General applications.
• Database systems.
• Spread sheets.
• Graphic packages.
• Word processors.
• Office automation.
• Scientific/engineering applications.
/************************************************************/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>
#include<stdio.h>
#include<stdlib.h>
/************************************************************/
/************************************************************/
class personal
char nm[20],fnm[20],cl[10],add[40],city[15],state[15],phc[10];
int age,dd,mm,yy;
float phn,zpcode;
public:
int rn;
void get(void);
void put(void);
};
/************************************************************/
/************************************************************/
class marks
char sb[10][10],gd,ans;
int m[10],mm[10],ts,tmm,tmo;
float av,per;
public:
int rn1;
void get1(void);
void put1(void);
void cal(int mm[],int ts,int m[]);
};
/************************************************************/
/************************************************************/
void personal::get(void)
cout<<endl<<setw(3)<<"Name : ";
cout<<endl<<setw(3)<<"Father's Name : ";
cout<<endl<<setw(3)<<"Roll No. : ";
cout<<endl<<setw(3)<<"Class : ";
cout<<endl<<setw(3)<<"Age : ";
cout<<endl<<setw(3)<<"D.O.B : "; gotoxy(22,10); cout<<setw(3)<<"/"
<< setw (3) <<"/" <<setw(3) <<" (dd/mm/yy)";
cout<<endl<<setw(3)<<"City : ";
cout<<endl<<setw(3)<<"State : ";
cout<<endl<<setw(3)<<"Zip/Postal Code : ";
cout<<endl<<setw(3)<<"Phone Code : ";
cout<<endl<<setw(3)<<"Phone no. : ";
gotoxy(22,5);
cin>>nm;
gotoxy(22,6);
cin>>fnm;
gotoxy(22,7);
cin>>rn;
gotoxy(22,8);
cin>>cl;
gotoxy(22,9);
cin>>age;
gotoxy(22,10);
cin>>dd;
gotoxy(25,10);
cin>>mm;
gotoxy(28,10);
cin>>yy;
gotoxy(22,11);
cin>>city;
gotoxy(22,12);
cin>>state;
gotoxy(22,13);
cin>>zpcode;
gotoxy(22,14);
cin>>phc;
gotoxy(22,15);
cin>>phn;
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
void marks::get1(void)
{
cout<<"Roll No. : ";cin>>rn1;
comp(rn1);
ans='y';
ts=0;
for(int i=0;ans=='y';i++)
{
ts++;
cout<<endl<<"subject"<<ts<<" : ";
cin>>sb[i];
cout<<"Marks Obtained in "<<sb[i]<<" : ";
cin>>m[i];
cout<<"Maximum Marks For "<<sb[i]<<" : ";
cin>>mm[i];
opt:
cout<<endl<<"Want to Add more Subject(s)--(y/n) : ";
cin>>ans;
if(ans!='y'&&ans!='n')
{
cout<<endl<<"\n\n\a !Incorrect entry press 'y' for yes and
'n' for no \a";
goto opt;
}
if(ans=='y'&&ts==10)
{
cout<<endl<<"\n\n\a!Space available for total no. of
subjects is full\a";
ans='n';
}
}
cout<<endl<<"Total no. of Subjects : "<<ts;
cal(mm,ts,m);
}
/************************************************************/
/************************************************************/
/************************************************************/
void marks::put1(void)
{
cout<<endl<<"Roll no. : "<<rn1;
cout<<endl<<"S.no."<<setw(8)<<"Subjects"<<setw(8)<<"Marks" ;
for(int i=1;i<=ts;i++)
{
cout<<endl<<i;gotoxy(9,i+3);cout<<sb[i-1];
gotoxy(25,i+3);cout<<m[i-1];
}
cout<<endl<<"Total no. of Subjects : "<<ts;
cout<<endl<<"Total Marks Obtained : "<<tmo;
cout<<endl<<"Total Marks : "<<tmm;
cout<<endl<<"Average Marks : "<<setprecision(2)<<av;
cout<<endl<<"Percentage Marks : "<<setprecision(2)<<per<<"%";
cout<<endl<<"Grade : "<<gd;
if(gd=='F')
cout<<endl<<"Remarks : FAIL";
else
cout<<endl<<"Remarks : PASS";
}
/************************************************************/
/************************************************************/
void add (void);
void mod (void);
void del (void);
void search (void);
void list (void);
void add1 (void);
void mod1 (void);
void del1 (void);
void search1 (void);
void list1 (void);
/************************************************************/
/************************************************************/
personal per;
marks mak;
ofstream f1;
ifstream f2;
fstream f;
/***********************************************************/
/***********************************************************/
void main()
{
xyz:
clrscr();
cout<<endl<<"1.Personal Details"<<endl<<"2.Marks
Details"<<endl<<"3.Exit";
int s;
cout<<endl<<"enter 1 for personal details , 2 for marks details and 3 to
exit : ";
cin>>s;
switch(s)
{
case 1:
{
abc:
clrscr();
cout<<endl<<"PERSONAL:";
cout<<endl<<"1.Add\n2.Modify\n3.Delete\n4.Search\n5.Li
st\n6.Back";
cout<<"\nenter the serial no. of the operation you want to
perform : ";
int e;
cin>>e;
switch(e)
{
case 1:
{
char rep='y';
while(rep=='y')
{
clrscr();
add();
cout<<"\nwant to add more record(s)-
(y/n)";
cin>>rep;
}
goto abc;
}
case 2:
{
clrscr();
mod();
goto abc;
}
case 3:
{
clrscr();
del();
goto abc;
}
case 4:
{
clrscr();
search();
goto abc;
}
case 5:
{
clrscr();
list();
goto abc;
}
case 6:
{
clrscr();
goto xyz;
}
default:
{
clrscr();
goto abc;
}
};
}
case 2:
{
pqr:
clrscr();
cout<<endl<<"MARKS:";
cout<<endl<<"1.Add\n2.Modify\n3.Delete\n4.Search
\n5.List\n6.Back";
cout<<"\nenter the serial no. of the operation you want to
perform : ";
int h;
cin>>h;
switch(h)
{
case 1:
{
clrscr();
add1();
goto pqr;
}
case 2:
{
clrscr();
mod1();
goto pqr;
}
case 3:
{
clrscr();
del1();
goto pqr;
}
case 4:
{
clrscr();
search1();
goto pqr;
}
case 5:
{
clrscr();
list1();
goto pqr;
}
case 6:
{
clrscr();
goto xyz;
}
default:
{
clrscr();
goto pqr;
}
};
}
case 3:
{
exit(0);
}
default:
{
clrscr();
goto xyz;
}
};
}
/************************************************************/
/************************************************************/
{
cout<<endl<<"PERSONAL:ADD:";
cout<<"\n\n";
f.open("mi.txt",ios::app);
per.get();
f.write((char *)&per,sizeof(per));
f.close();
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
void search(void)
{
int c,k;
f2.open("mi.txt");
char ans='y';
while(ans=='y')
{
cout<<"enter roll no. whose record is to be searched : ";
cin>>c;
clrscr();
cout<<endl<<"PERSONAL:SEARCH:";
cout<<"\n\n";
k=0;
while(f2.read((char *)&per,sizeof(per)))
{
k=0;
if(per.rn==c)
{
per.put();
k=1;
break;
}
}
if(k==0)
cout<<"\n\a!Record DOES NOT Exist\a";
cout<<"\nwanna search more records--(y/n)";
cin>>ans;
f2.close();
}
}
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
void search1(void)
{
int c,k;
f2.open("mi.txt");
char ans='y';
while(ans=='y')
{
cout<<"enter roll no. whose record is to be searched : ";
cin>>c;
clrscr();
cout<<endl<<"MARKS:SEARCH:";
cout<<"\n\n";
k=0;
while(f2.read((char *)&mak,sizeof(mak)))
{
k=0;
if(mak.rn1==c)
{
mak.put1();
k=1;
break;
}
}
if(k==0)
cout<<"\n\a!Record DOES NOT Exist\a";
cout<<"\nwanna search more records--(y/n)";
cin>>ans;
f2.close();
}
}
/************************************************************/
/************************************************************/
void del1 (void)
{
int c;
f1.open("new.txt");
f2.open("mi.txt");
char ans='y';
while(ans=='y')
{
cout<<endl<<"MARKS:DELETE:";
cout<<"\n\n";
cout<<"enter roll no. whose record is to be deleted : ";
cin>>c;
clrscr();
cout<<endl<<"MARKS:DELETE:";
cout<<"\n\n";
while(f2.read((char *)&mak,sizeof(mak)))
{
if(mak.rn1!=c)
{
f1.write((char *)&mak,sizeof(mak));
}
}
f1.close();
f2.close();
remove("mi.txt");
rename("new.txt","mi.txt");
cout<<"\nwanna delete more records--(y/n)";
cin>>ans;
f.close();
}
}
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/
/************************************************************/