1 Object Oriented Programming(OOPS)



Introduction to C++:
C++ was developed by Bjarne Stroustrup at Bell Lab in 1983.  All the keywords in C are the keywords in C++ with some new keywords.  The Name C++ comes from the increment operator of C Language.  C++ is not merely an extension of C, where some new syntax has been added basic purpose of C++ is to add features to the standard C language supporting use to Object Oriented Programming.

Additional Keywords in C++:
class                            friend                          virtual                          inline              
private                         public                          protected                    const
this                              new                             delete                         operator

Comments:
Comments are the non executable statements.  In C as well as C++  // is used for single line comment and /*- - - - - - - - - - - - - - - - - - */ is a multiple line statement comment.

Variable Declarations:
In C++ declarations of variable can be done with executable program statements.

Example 1
#include <stdio.h>
int main(void){
int x=10;
printf(“Value of X%d\n””,x);
int y=0;
for(int z=0; z<<; z++)
{
printf(“\nz =%d”,z);
z++;
x++;
y--;
}

Scope Resolution Operator(: :):
C++ provides the scope resolution operator to access global variables.  Thus overriding a local variable with the same name.  This operator prefixed to the name of the global variable.

Example 2
#include<stdio.h>
int global =10;
void main  ( ){
int gloabal =20;
printf(“Writing Global Prints%d\n”,global);
printf(“Writing Global Prints%d\n”,:: global);
}

New and Delete Operators:  
New operator allocates memory for the given size and returns a pointer to its starting point.  And delete operator releases the memory which is allocated by new e.g.
char arr[100];
arr = new char[size];

It is possible that the system may not have enough memory available to satisfy a request by new operator and so it returns a Null pointer.  So returned value from new should always be checked before any operator on it, which otherwise may lead to advertent system crashes.  Once memory is allocated by new operator it can be accessed using indirection operator(*) or array subscripts.

Practical Session  1

Example 1(WS Manipulator)
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
main(void){
char name[100];
clrscr( );
cout<<"Enter Line of text \n  " ;
cin>>ws;
cin>>name;
cout<<"Typed text ="<<name<<endl;
getch( );
}
Example 2(Set Precision Manipulator)
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
main(void){
float a,b,c;
a=5;
b=3;
c=a/b;
clrscr( );
cout<<setprecision(1)<<c<<endl;
cout<<setprecision(2)<<c<<endl;
cout<<setprecision(3)<<c<<endl;
getch( );
}
Example 3(SetW and Sefill Manipulator)
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
main(void){
int a,b;
a=200;
b=300;
cout<<setfill("*");
cout<<setw(5)<<a<<setw(5)<<b<<endl;
cout<<setw(6)<<a<<setw(6)<<b<<endl;
cout<<setw(7)<<a<<setw(7)<<b<<endl;
cout<<setw(8)<<a<<setw(8)<<b<<endl;
}

Example 4(Ends Manipulator)

#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
main(void){
int number=123;
clrscr( );
cout<<'\"'<<"number =" <<number<<ends;
cout<<'\"'<<endl;
getch( );
}
Example 5(Reading a String)
#include<iostream.h>
#include<conio.h>
void main(void){
char ch;
clrscr( );
cout <<"\n Enter a Line :";
for( ; (ch =cin.get( ))!='\n'; ){
cout<<"\n Your character is : ";
cout.put(ch);
}
getch( );
}
Example 6 (Fill and Width)
#include<iostream.h>
#include<conio.h>
void main(void){
clrscr( );
cout.width(10);
cout.fill('x');
int num=6;
cout<<num<<"\n";
cout.width(15);
cout.fill('-');
num=12345;
cout<<num<<endl;
getch( );
}
Example 7(Field Justification)
#include<iostream.h>
#include<conio.h>
void main(void){
int num =71;
clrscr( );
cout.fill('*');
cout.setf(ios::showpos);
cout.setf(ios::left,ios::adjustfield);
cout.width(6);
cout<<num<<"\n";
cout.setf(ios::right,ios::adjustfield);
cout.width(6);
cout<<num<<"\n";
cout.setf(ios::internal,ios::adjustfield);
cout.width(6);
cout<<num<<"\n";
getch( );
}
Example 8(Printing Numbers from 0 to 9)
#include<iostream.h>
#include<conio.h>
void main(void){
int i;
clrscr( );
for(i=0;i<=9;i++)
{
cout<<"\n i = "<<i;
}
getch( );
}
Example 9(A Structure Program)
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct student{
            int rollno;
            char name[25];
            char branch[30];
            int marks;
            };
void main( )
{
clrscr( );
student s1;
cout<<"\n Enter Student Data : ";
cout<<"\n Enter Rollno       : ";
cin>>s1.rollno;
cout<<"\n Enter Student Name : ";
cin>>s1.name;
cout<<"\n Enter Study Branch : ";
cin>>s1.branch;
cout<<"\n Enter Total Marks  : ";
cin>>s1.marks;
cout<<"\n\n\n\t\t\t\t Student Report ";
cout<<"\n\t\t\t ***************************";
cout<<"\n Rollno \tName \t\tBranch \t\tMarks \t\tPercent ";
cout<<"\n "<<s1.rollno<<"\t\t"<<s1.name<<"\t\t"<<s1.branch<<"\t"<<s1.marks<<"\t\t"<<s1.marks*(100.0/325);
getch( );
}
Example 10(Calculation of Cirmferance of a Circle)
#include<stdio.h>
#include<conio.h>
main( ){
clrscr( );
float radius,circum,area;
float pi=3.1416;
printf("\nEnter value of the Radius :");
scanf("%f",&radius);
circum=2*pi*radius;
area=pi*radius*radius;
printf("\nThe Circumferance of the Circle :  %f",circum);
printf("\nThe Area of the Circle : %f",area);
getch( );
}

Top

No comments:

Post a Comment