Pages

Write program to store data in file object oriented approch.

Tuesday, 25 June 2013

#include <iostream>
#include <string.h>
#include <fstream>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void save()// dada save in file
      {
           ofstream outfile;  // create object of of class
           outfile.open("abs.txt");  //open file inwhich data will be stored
           outfile<<"Your name is "<<name<<"\nYour age is "<<age<<endl;  // data is storing in file.
           cout<<"saved\n";
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.save();
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}

Create a class of student, get student name and age and print on screen using setter and getter function.

#include <iostream>
#include <string.h>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}

Write a program to print counting from 1 to 10 using for loop

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    // The loop goes while x < 10, and x increases by one every loop
    for ( x = 0; x < 10; x++ ) {
        /* Keep in mind that the loop condition checks 
           the conditional statement before it loops again.
           consequently, when x equals 10 the loop breaks.
           x is updated before the condition is checked. */   
        printf( "%d\n", x );
    }
    getch();
}

write a program to print Fibonacci series till given number.

Friday, 7 June 2013

#include <stdio.h>
#include <conio.h>
int fibn(int n)
{
      int a, b, c;
      if(n==1 || n==2)
      return(n-1);
      a=fibn(n-1);
      b=fibn(n-2);
      c=a+b;
      return c;
}
int main()
{
    int fib, i;
    printf("Enter ending number where fibanoci series ends: ");
    scanf("%d", &fib);
    for(i=1; i<=fib; i++)
    {
             printf("Fibnocii of %d is %d.\n", i, fibn(i));
    }
    getch();
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *