Showing posts with label CPP. Show all posts
Showing posts with label CPP. Show all posts

Monday, June 19, 2023

Pointer in CPP

 

Pointer

Pointer points address of another variable

syntex

datatype  *variablename=value;

eg. int  *a=5;

int *b=&a;




  Example:

int a;

int  *ptr;

a=10;

ptr=&a;



100     - address of a         200  -  address of  ptr



             value of a                           value of ptr

a                                                             ptr

 

-a                          refers to 10

-&a                       refers to 100

-*(&a)                  refers to 10

-ptr                       refers to 100

-&ptr                    refers to 100

-*ptr                     refers to 10

 

 

program:1

#include<iostream.h>

#include<conio.h>

void main()

{

int i,*iptr;

iptr=&i;

i=20;

clrscr();

cout<<" i value is "<<i<<endl;

*iptr=*iptr+30;

cout<<"i value is "<<i<<endl;

getch();

}

output:

i value is 20

i value is 50

 

program:2

#include<iostream.h>

#include<conio.h>

void main()

{

int arr[5]={10,20,30,40,50};

int  *ptr,i;

ptr=arr;

clrscr();

for(i=0;i<5;i++)

{

         cout<<*(ptr+i)<<"\t";

         *(ptr+i)=*(ptr+i)+5;

}

cout<<endl;

for(i=0;i<5;i++)

{

                  cout<<arr[i]<<endl;

}

getch();

}

output:

10 20 30 40 50

15 25 35 45 55


program:3

#include<iostream.h>

#include<conio.h>

void main()

{

char name[10];

char *cptr;

clrscr();

cout<<"enter the name in lower case:";

cin>>name;

cptr=name;

for(;*cptr!='\0';cptr++)

{

   if(*cptr>='a' && *cptr<='z')

                *cptr=*cptr-32;

}

cout<<" upper case is : "<<cptr<<endl;

}

output:

Enter the name in lower case : apple

Upper case is : APPLE

VIRTUAL FUNCTIONS and POLYMORPHISM

VIRTUAL FUNCTION AND POLYMORPHISM 



EXAMPLE

#include<iostream.h>

#include<conio.h>

class base{

                        public:

virtual void show()

                 {

                 cout<<"base class";

                }

         };

class derived : public base

              {

              public:

              void show()

             {

            cout<<"Derived";

            }

            };

void main()

{

base *p;

p=new base();

p->show();

p=new derived();

p->show();

getch();

}

output

base class derived

 

Pure virtual function

syntax [=0]

class a{

             public:

            virtual void fun()=0;

           };

ABSTRACT CLASS

a class which consist atleast one pure virtual function is called an abstract base class. an abstract class is used as a base class for deriving purpose only .it could not be used for creating object.

 

 

 

Files in Cpp

 

Files

Files store data permanently in a storage device. With file handling, the output from a program can be stored in a file.

#include<fstream.h>

 


syntax

open (file_name, mode);
 

Value

Description

ios:: app

The Append mode. The output sent to the file is appended to it.

ios::ate

It opens the file for the output then moves the read and write control to file’s end.

ios::in

It opens the file for a read.

ios::out

It opens the file for a write.

ios::trunk

If a file exists, the file elements should be truncated prior to its opening.

 

C++ Files and Streams

Data Type

Description

fstream

It is used to create files, write information to files, and read information from files.

ifstream

It is used to read information from files.

ofstream

It is used to create files and write information to the files.


C++ FileStream: writing to a file

#include<iostream.h>

#include<fstream.h>

#include<conio.h>

void main()

{

int empno,empsal;

char empname[25];

clrscr();

ofstream fp;

fp.open("file.txt");

cout<<"enter employee number :";

cin>>empno;

cout<<"enter employee name:";

cin>>empname;

cout<<"enter employee salary:";

cin>>empsal;

fp<<empno<<"\n";

fp<<empname<<"\n";

fp<<empsal<<"\n";

fp.close();

getch();

}

Output:

Check the output on file path text file: C:\TurboC++\Disk\TurboC3\BIN

5

lily

1000

C++ Read a File

#include<iostream.h>                       

#include <fstream.h>      

#include<conio.h>

void main() {

    fstream FileName; 

    clrscr();

    FileName.open("cs.txt", ios::in);        

    if (!FileName) {                       

        cout<<"File doesn’t exist.";         

    }

    else {

        char x;                    

        while (1) {        

            FileName>>x;             

            if(FileName.eof())         

                break;             

            cout<<x;                 

        }

    }

    FileName.close();                  

   getch();

}

output



C++ Read and Write 

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<fstream.h>

void main()

{

    char string[80];

    clrscr();

    cout<<"Enter a string";

    cin>>string;

    int len=strlen(string);

    fstream file;

    file.open("sample.txt",ios::in|ios::out);

         for(int i=0;i<len;i++)

         file.put(string[i]);

         file.seekg(0);

         char ch;

         while(file)

         {

             file.get(ch);

             cout<<ch;

         }

         getch();

}

Output:



C++ Append

#include<iostream.h>

#include<fstream.h>

#include<conio.h>

void main()

{

 

    char str[50] = "Welcome to Program techie!";

    char ch;

    fstream fstream_ob;

    fstream_ob.open("File.txt", ios::app);

    fstream_ob<< str << "\n";

    fstream_ob.close();

    getch();

}

Check the output on file path text file: C:\TurboC++\Disk\TurboC3\BIN

Output

Welcome to Program techie!