Anda di halaman 1dari 11

c Ê 

  
   
   
 
 
In C++, a function can call itself. A function is said to be recursive if a statement in the body of the function calls
itself. Recursion is the process of defining something in terms of itself, and is sometimes called circular definition.
#include<stdio>
using namespace std;
int factr(int n);
int main(int argc, char *argv[]){
int factr(int n) {
int answer;
if(n==1) return(1);
answer = factr(n-1)*n;
return(answer);
}
}

G Ê  
    



 
 






#include<stdio>
using namespace std;
void main()
{
int a.b,c,n;
a=0,b=1;
cout<<"ENTER THE LIMIT TILL WHERE TO CONTINUE TH SERIES";
cin>>n;
cout<<a<<"\t"<<b;
while(c<=n)
{
c=a+b;
a=b;
b=c;
cout<<"\t"<<c;
}

r Ê   








When you precede a member variable's declaration with static, you are telling the compiler that only one
copy of that variable will exist and that all objects of the class will share that variable. Unlike regular data
members, individual copies of a static member variable are not made for each object. No matter how many
objects of a class are created, only one copy of a static data member exists. Thus, all objects of that class use
that same variable. All static variables are initialized to zero before the first object is created. Write a
program to check whether a given year is leap or not using ternary operator.

#include <iostream>
using namespace std;
class shared {
static int a;
int b;
public:
void set(int i, int j) {a=i; b=j;}
void show();
};
int shared::a; // define a
void shared::show()
{
cout << "This is static a: " << a;
cout << "\nThis is non-static b: " << b;
cout << "\n";
}
int main()
{
shared x, y;
x.set(1, 1); // set a to 1
x.show();
y.set(2, 2); // change a to 2
y.show();
x.show(); /* Here, a has been changed for both x and y
because a is shared by both objects. */
return 0;
}

 Ê  










Inheritance is the process by which new classes called derived classes are created from existing classes
called base classes. The derived classes have all the features of the base class and the programmer can choose
to add new features specific to the newly created derived class. The different types of inheritences are :






 




 






 


 Ê       






In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the
characteristic of being able to assign a different meaning or usage to something in different contexts -
specifically, to allow an entity such as a variable, a function, or an object to have more than one form. A real-
world example of polymorphism is a thermostat. No matter what type of furnace your house has (gas, oil,
electric, etc.), the thermostat works the same way. In this case, the thermostat (which is the interface) is the
same no matter what type of furnace (method) you have. For example, if you want a 70-degree temperature,
you set the thermostat to 70 degrees. It doesn't matter what type of furnace actually provides the heat.

o Ê  



 
In overloading,there is a relation ship between methods available in the same class where as in
overridding,there is relationship between a super class method and subclass method. overloading doesn't
block inheritence from the superclass where as overridding blocks inheritence. in overloading,seperate
methods share the same name where as in overridding,subclass methods replaces the superclass.
overloading must have different method signatures where as overriding must have same signature

X Ê 
 

 
  

 
 

A stream is a logical device that either produces or consumes information. A stream is linked to a physical
device by the I/O system. All streams behave in the same way even though the actual physical devices they are
connected to may differ substantially. Because all streams behave the same, the same I/O functions can operate
on virtually any type of physical device. For example, you can use the same function that writes to a file to write
to the printer or to the screen. The advantage to this approach is that you need learn only one I/O system.

 Ê  







 
   


A data structure is linear if every item is related (or attatched) to its previous and next item(e.g.array, linked list)
and it is non-linear if every item is attached to many other items in specific ways to reflect relationships(e.g, n-ary
tree). In linear data structure data items are arranged in a linear sequence. In non-linear data structure data items
are not in a sequence. Linear data structure are stack, queue, array whereas non linear data structure are graph
and tree.

Ë Ê 

 
 
 


 

âueues are a type of container adaptors, specifically designed to operate in a FIFO context (first-in first-out), where
elements are inserted into one end of the container and extracted from the other. âueues are implemented
as containers adaptors, which are classes that use an encapsulated object of a specific container class as
its underlying container, providing a specific set of member functions to access it elements. Elements
are pushed into the "back" of the specific container and popped from its "front".

#include <iostream>
using namespace std;

#define SIZE 100

class âueueClass {
int queue[SIZE]; // holds the queue
int head, tail; // indices of head and tail
public:
void init(); // initialize
void q(int num); // store
int deq(); // retrieve
};

void âueueClass::init()
{
head = tail = 0;
}

void âueueClass::q(int num)


{
if(tail+1==head || (tail+1==SIZE && !head)) {
cout << "âueue is full\n";
return;
}
tail++;
if(tail == SIZE)
tail = 0; // cycle around
queue[tail] = num;
}

int âueueClass::deq()
{
if(head == tail) {
cout << "âueue is empty\n";
return 0; // or some other error indicator
}
head++;
if(head==SIZE) head = 0; // cycle around
return queue[head];
}

int main()
{
âueueClass queue1, queue2;
int i;

queue1.init();
queue2.init();

for(i=1; i <=10; i++) {


queue1.q(i);
queue2.q(i*i);
}

for(i=1; i <=10; i++) {


cout << "Dequeue 1: " << queue1.deq() << endl;
cout << "Dequeue 2: " << queue2.deq() << endl;
}

return 0;
}

Part Ȃ B
c Ê         ! 
 





String objects are a special type of container, specifically designed to operate with sequences of characters. Unlike
traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a
class with many built-in features to operate with strings in a more intuitive way and with some additional useful
features common to C++ containers.

Four string handling functions :

insert( ), erase( ), replace( ), assign().

#include <iostream>

#include <string>

using namespace std;

int main()

string str1("String handling C++ style.");

string str2("STL Power");

cout << "Initial strings:\n";

cout << "str1: " << str1 << endl;

cout << "str2: " << str2 << "\n\n";

// demonstrate insert()

cout << "Insert str2 into str1:\n";

str1.insert(6, str2);

cout << str1 << "\n\n";

// demonstrate erase()

cout << "Remove 9 characters from str1:\n";

str1.erase(6, 9);

cout << str1 <<"\n\n";

// demonstrate replace

cout << "Replace 8 characters in str1 with str2:\n";

str1.replace(7, 8, str2);

cout << str1 << endl;

return 0;

cc Ê 
    
    
   
#include<conio.h>

#include<iostream.h>

void main()

clrscr();

int a[100],i,n,j,temp;

cout<<"How many element: ";

cin>>n;

cout<<"Enter the elements of array: "<<endl;

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

cin>>a[i];

cout<<"The elements of array Before Sorting: "<<endl;

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

cout<<a[i]<<" ";

cout<<endl;

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

for(j=0;j<n-1-i;j++)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

cout<<"Elements of array After Sorting: "<<endl;

for(i=0;i<n;i++)
cout<<a[i]<<" ";

getch();

cG Ê  
""# 
  

""# 
Object-oriented programming (OOP) is a programming language model organized around "objects" rather
than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure
that takes input data, processes it, and produces output data.
The programming challenge was seen as how to write the logic, not how to define the data. Object-oriented
programming takes the view that what we really care about are the objects we want to manipulate rather
than the logic required to manipulate them. The features of OOPs are :

!   

Abstraction is a process of identifying the relevant qualities and behvaiors an object should possess. In object-
oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying
the critical behavior of an object and eliminating irrelevant and complex detilals. A well thought-out abstraction is
usually simple, and easy to use in the perspective of the user, the person who is using your object.

  

Encapsulation is a method for protecting data from unwanted access or alteration by packaging it in an object
where it is only accessible through the object's interface. Encapsulation are often referred to as information hiding.
But both are different. Infact information hiding is actually the result of Encapsulation. Encapsulation makes it
possible to separate an object's implementation from its orgiinal behavior - to restrict access of its internal data.
This restriction facilitate certains detiails of an object;s behavior to be hidden. This allows to protect an object's
interal state from corruption by its user. It is the mechanism by which Abstraction is implemented. In other words
you can say that it is the result of the Encapsulation.

$




Inheritecne is the ability to define a new class or object that inherits the behavior and its functionality of an
existing class. The new class or obejct is called a child or subclass or derived class whie the original class is called
parent or base class.

#  

As name suggests, Polymorphism means an ability to assume different forms at different places. In OOP, it is a
language's ability to handle objects differently based on their runtime type and use. Polymorphism is briefly
described as "one interface, many implementations".Ppolymorphism is a characteristic of being able to assign a
different meaning or usage to something in different contexts specifically, to allow an entity such as a variable, a
function, or an object to have more than one form.

There are two types of polymorphism.


1.Ê Compile time polymorphism - It is achieved by overloading functions and operators
2.Ê Run time polymorphism - It is achieved by overriding virtual functions

Lets say we have a class that have many Load methods having different parameters, this is called Compile time
polymorphism. Lets take another example where we have a virtual method in the base class called Load with one
parameter and you have redefined its functioanlity in your sub class by overriding base class Load method, this is
called Run time polymorphism.

cr Ê 
  

 





#include <iostream>

using namespace std;

class loc {

int longitude, latitude;

public:

loc() {} // needed to construct temporaries

loc(int lg, int lt) {

longitude = lg;

latitude = lt;

void show() {

cout << longitude << " ";

cout << latitude << "\n";

loc operator++();

};

loc loc::operator++()

longitude++;
latitude++;

return *this;

int main()

loc ob1(10, 20), ob2( 5, 30), ob3(90, 90);

ob1.show();

ob2.show();

++ob1;

ob1.show(); // displays 11 21

ob2 = ++ob1;

ob1.show(); // displays 12 22

ob2.show(); // displays 12 22

return 0;

c Ê%
  


 
  
   
 %
  

A linked list is a technique of creating a list with the ability to add, delete, or retrieve items. Additional operations
can also be provided to a more elaborate list such as finding an item, deleting an item. In other words linked list
can be defined as a data structure which is built from structures and pointers. It forms a chain of "nodes" with
pointers representing the links of the chain and holding the entire thing together.

#include <iostream>

using namespace std;

const int SIZE = 10;

// Create a generic stack class

template <class StackType> class stack {

StackType stck[SIZE]; // holds the stack

int tos; // index of top-of-stack

public:
stack() { tos = 0; } // initialize stack

void push(StackType ob); // push object on stack

StackType pop(); // pop object from stack

};

// Push an object.

template <class StackType> void stack<StackType>::push(StackType ob)

if(tos==SIZE) {

cout << "Stack is full.\n";

return;

stck[tos] = ob;

tos++;

// Pop an object.

template <class StackType> StackType stack<StackType>::pop()

if(tos==0) {

cout << "Stack is empty.\n";

return 0; // return null on empty stack

tos--;

return stck[tos];

int main()

// Demonstrate character stacks.

stack<char> s1, s2; // create two character stacks

int i;
s1.push('a');

s2.push('x');

s1.push('b');

s2.push('y');

s1.push('c');

s2.push('z');

for(i=0; i<3; i++) cout << "Pop s1: " << s1.pop() << "\n";

for(i=0; i<3; i++) cout << "Pop s2: " << s2.pop() << "\n";

// demonstrate double stacks

stack<double> ds1, ds2; // create two double stacks

ds1.push(1.1);

ds2.push(2.2);

ds1.push(3.3);

ds2.push(4.4);

ds1.push(5.5);

ds2.push(6.6);

for(i=0; i<3; i++) cout << "Pop ds1: " << ds1.pop() << "\n";

for(i=0; i<3; i++) cout << "Pop ds2: " << ds2.pop() << "\n";

return 0;

Anda mungkin juga menyukai