Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views4 pages

Opps Cheat

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

6.2] Explain Basic Concepts of OOPS Q4)What are different tokens in C++? Q6] what is type conversion?

What are different tokens in C++? Q6] what is type conversion? state it's type and Explain with example.
→1)Oops is an approach or a programming patters where the programs are structured →1)The smallest individual units in a program are called tokens. It is a Common name given → When variables of one type are mixed with variable of another type, a type Conversion
around objects rather than functions and logic. 2) OOPS is a paradigm many concepts such to similar items. will occur.
as Inheritance Data minding, polymorphism, Encapsulation, Aistraction. 3) when one object 2) C++ has following tokens.
There are two types of type Conversion
acquires all the properties and behaviours of parent object i-e known as Inheritance 4) when
[(a) Keywords -- Keywords are these word where meaning has already been explained to the
one task is performed by different ways be known as polymorphism 5) Abstraction is hiding 1) Implicit Type conversion. *2) Explicit type conversion.
compiler. The keywords implements specific feature of the language. The keyword coud be
internal details and showing functionality 6) Binding code and data together into a single
und as a variable name, eg- if, else, int, long, switch i)Implicit This type conversion is carried by the C++ compiler automatically.
unit is known as encapsulation.
(b) Identifires - Identifires are the unique names given to Variable, classes, function or other example: short a = 10;
Q3] Explain applications and benefits of OOPS.
entities by the programmer . int 6 = a;
→Applications:-
(c) Constants - A constant is a quantity that does not change. A constant ;. é a quantity can Here, variable a is declared to be a short integer
1) Real time system design
be stored at locations in the memory of the Computer..
"It inherits complexities and makes it difficult to build them. OOP techniques make it easier WADE NO. 8
to handle Those complexities. (d) Operators - An operator is a symbol that tells the computer too perform certain
After assigning a to b, the value of promoted to integer from Short by the compiler
mathematical and logical manipulations.
2) Hypertext and Hypermedia automatically without us having to tell it to do so explicitly
Hypertext is similer to regular text As it can be stored, search & edited easily (e)string - A string is set of characters enclosed in double A string may consist of any
1) Explicit
character from character set
Hypermedia on the other hand is a superset of hypertext. OOP also helps in laying the
Not all converstions can be automatically done by the compiler. Explicit conversions allow us
framework for hypertext and hypermedia. Q.5] Explain different data. Type in c++
toe force conversions when normally it I cannot be carried out by the compiler
3) AI Expert System: These are computer application that is developed to solve complex → 1) Built in data types. types.
automatically for implicitly)
problems which are far beyond the human brain. oop helps to develop such Al expert Built in type is also called as Basic or Primary data type.
system. Example-
(a) Integer *(b) Character *(c) Float *(d) Double *(e) void
4) Client-server system double x = 1.2;
2) User defined data types.
Object-oriented client Server system provide the IT infrastructure creating object-oriented int sum = (int) x + 1;
server interact (OSCI) applications. The user defined Data type defined by user in the programs as per need of. the cout << " Sum = " << sum;
5) Object-Oriented database: The databases try to maintain a direct correspondence programmer. This feature of adding own data type makes the C++ on extensible languge return 0;
between the real world and database object in order to let the object retain it. identity and (a) Structure *(b) Union *(C) Class *(d) Eumeration.
Sum=2
integrity
3) Derived data types
Here variable x is first declared double and is function sum it is added by declaring sum as
Benefits:
The derived data types are also called as structured data types or secondary data types they int So the function adds only integer value and gives output accordingly
1) The objects in OOP have the state and behaviour. That is similar to the real world object are formed by using basic data types of the language.
2) It is more Suitable for large projects (3) The projects executed using oop technique are (A)Array (B) Function (C) Pointer
more reliable 4) OOP divides the problems into objects to provide service to solving
problems. 5)Object oriented systems are easier to upgrade modify 6) The Concept of OOP
also enhances the reusability 7) Software complexity can be better! 3) Principle of data
hiding helps e managed programmer to design and develop safe program. (9) The Concept
like inheritance and polymorphism provide the extensibility by OOP languages.

Q7] what are different types of control statements? Q9) what is away? How to create 2D-array in C++? Explain with Suitable example.
→ Control statements help with the control flow of the program, when we want a particular Q1) How to define member function inside and outside the class?
→ (1) Array is a collection of data of Same data type. It is used to store group of data
block of code to only be executed when it fulfills a specific condition or I wants to run a → Member functions of a class can be defined either outside the class or inside the class. to
simultaneously We cannot fetch data from array directly therefore we use index point The
block of code a specified number times we call that part of code as statements. These both cases, the function body remains the same. however the function header is different
in dering of array always start with D. index value is always integer number. arrays are used
statements will ensure a smooth and intentional flow of a program
to store multiple values in a single variable, instead of declaring separate variable Start Outside the class: Defining a member function outside a class requires the function
There are three types of control statements. declaration to provided inside the class definition. This declaration informs. the function is a
2)There are 2 types of array. i) Single dimensional array ii) Double dimensional array.
The compiler that member of the class and that it has been defined outside The class. The
1)Conditional/Selection statements.
Syntax of 2 dimensional array definition of member function outside the class defined from normal function definition, as
These control statements help with implementing decision-making is a program. A
the function name is the fumction header is preceded by the class name and the scope
conditional statement will select the block of statements that will execute based on the
resolution operator(::)
given condition. If the condition provided is true the code will be executed otherwise, it will
not. i) Simple if ii) if else. Wii) Else if iv) Nested if else. The scope resolution operator in forms the compiler what class the member belongs to.
Q)what is string? Explain various methods of string.
PACE NO: 10 Syntax:
→1) string is a sequence of characters stored in a contiguous block of memory, which
2)Iterative statement /looping statements Return_type class name:; function name (argument declaration)
provides various methods to manipulate string 2) This string is actually of character which is
Iterative statements are also know as repetitive statements or Looping statements. A
terminated by a null character {
looping statement is used when the program requires a statement's execution until some
condition for loop termination is satisfied. for loop i) while loop. in) do what 3) Various methods of string. // body of the member function
I) strncpy --- Copies a given number of characters of one string to another
3)Jump statements }
(ii) strcpy --- Copies one string to another.
Jump statements are statements which we can transfer control anywhere in the program
iii) strlen --- Finds lingth of a string Inside the Class: A member function of a class can also be defined inside the class. However,
These statements will transfer program control by skipping Statements that don't have
(iv) strcmp Compares two strings. when a member function is defined inside the class, "the class name and the Scope
Conditions Specified Break 11) Continue iii) Pass
v)streat - appends one string to another. resolution operator are not specified in the function header Moreover, The member
Q.8)write short note on UML. (vi) strchar Finds first occurreme of a given character in a string. functions defined inside a class definition are by default online intern functions.
→ 1)Unified Modeling Language (UML) is a general purpose Language. The main aim of UML
example. class book {
is to define a standard way to Visualize the way system has been designed. 2)UML is not a
char title [30] ;
programming language it is rather a visual Language. 3) UML is linked with object oriented
float price;
design and analysis. UML makes the we of elements and forms associations. between them
public :
to form diagrams. 4) It is analogous to the blue prints used in other fields, and consist of
void getdata (char [], float);
different types of diagrams 5) UML is a combination of several object-oriented notations: let's see another example with distructor void putdata ( )
Object. oriented design, object Modeling Technique, Object oriented software engineering implementation {
Cout <<"\n Title of Books : << title ;
#include<iostream> cont <<"I am inside destructor
6 )Types of UML diagrams. cout <<"\n Price of Books "<<price:
using namespace \n";
UML uses elements and associates them in different ways to form diagrams that represent std; } };
static, or structural aspects of a system, and behavioral diagrams. Class };
Demo int main() In this example, the member function putdata () is defined the class book Hence, putdata ()
a) Structural class diagram. 1) Class 2) Component 3) composite structure 4)Deployment
{ { is by defined an inline function.
5)object vi) Package diagram
public Demo obj ;
b) Behavioral UML diagrams 1) Activity Diagram 2) Communication 3) Interaction 4) Demo() // }
sequence (5) State 6) Timing 7) use case. constructor O/P I am inside constructor * I am inside
{ destructor.
Cont <<" I am inside Constructr
\n”;
}
~ Demo() // destructor
Q.2) what is constructer and destructer? write a program for constructor overloading and
destructor implementation.
Q3] Impliment array of objects. Q4) what is use of static data members and member functions?
→1)Constructor is a special member -function where task is to initialize the object of its
→ 1) When a class is defined, only specification for the object is defined no memory or →a) 1) A data member of a class can be qualified as a state. The properties of a "static
class. It is special because its name is same as the class name 2) Constructor function have
storage is allocated to use the data and access function defined in the class, you need create member variable are similar to that of a C static variable. A static data members are class
some special characteristics. i) They should be declared in the public section. ii) They are
objects. 2) The array of Objects stores objects an array of a class type is also known as an members that are declared wing static keywords. 2)only one copy of that member is created
invoked automatically when the object are created iii) They do not have the return types
array of objects. 3) Let's see an example of Storing more than one Employee data. Let's for the entire class and is shared by all the objects of that class no matter how many objects
therefore cannot returns the value. iv) They can have the default arguments. v) Constructor
assume there is an array of objects for storing employee data emp [50] created. 3) it is initialized before any object of this class is created, even before The main
cannot be virtual. vi) An object with a constructor (or destructor) cannot be used as a
starts. It is visible only within the class, but its lifetime is The entire program.
member of a union. Objects Employee id Employee name 4)syntax
3)3 types of constructor Static data type data_member_name;
emp [0] →
i) Parameterized constructor ii) Default constructor iii) copy b) 1)Static member functions are special functions used to access the static data members
emp [1] →
4) Destructor- or other static member functions, defined by Static keyword.2)static member function. The
emp [2] →
i) It is a special member function of class that executes when the work of object goes out of single copy of The member function to any number of the class object 3)we can access it via
emp [3] →
scope. ii) This same as class name iii) It has no return type and awe parameter iv) It is used class name or clase objects. If the static member function accesses any non- static data
to destroy class level variable (v) Tilde symbol(~) is used to declare destructor. Program cout <<endl; member or non-static member function, it throws an error!
5) Constructer Overloading. :- } 4)Syntax
A class with some name, as long as each has a different test of arguments This concept is #include <iostream> intmain Class name:: function_name (parameter)
known as Constructor overloading. It is similar to function overloading. using namespace std; {
classEmployee Employee_emp[30]; Q7]Explain generalization-specialization with suitable example.
*Program
{ → 1) The of extracting common characteristics from two or more claves and combining
#include <iostream> { int id; int n,i ; them into a Superclass is called generalization. The common characteristic can be attributes
using namespace std; cout << area << endl ; charname[30]; cout <<" Enter Number of Employees- “ ; or method. 2) Specialization is the reverse process
class } public: cin>>n; of Generalization means creating new Subclasses from an existing class. Specialization is the
construct } voidgetdata(): for(i=0;i<n;i++) process of dividing a parent level entity into narrower categories accordingly to all the
{ ; void putdata(): emp [1]. get data (); possible child categories.
public: int main() }; cout <<” Employee Data – “<<endl ; #include <iostream> {
float area voidemployee::getdata() for (i=0; i<n;i++) Class Animal Std:cout << "double:" << x << std ::endl;
; // Constructor Overloading { emp [i] putdata (); public virtual void speak ( )=0 ; }
// constructor with no // with two different constructors. Cout <<”Enter Id :”; } } template <typename T>
parameters // of class cin >>Id; Output : Class Dog public Animal {
constructor () name cout <<"Enter Name :" ; enter Number of Employees – 3 { Std :: cout <<”Generic :”<<x<< std :: endl;
{ Construct o ; cin >> name ; Entre Td: 101 public: }
area = 0; Construct 02 (10, 20); } -//- Name: Mahesh void Speak ( )override template < >
} o.disp() void Employee :: putdata () -//-Id: 102 { void print <std::string (std::string x)
// constructor with 2 parameters construct ; { -//-11- Name: Suresh Std:: cout << woof!" <<std::endl; {
(int a int b) 02. disp () ; Cout<<id <<" “; -//-Id: 103 } std::cout << "string"" << x << std::endl ;
{ return 1 ; cout << name << " “ ; ; -//- Id Name: Mayuresh }; }
area = a *b; } Employee Data – void print (int x) int main()
} Output : 0 101 Mahesh { {
void disp ( /200 102 Suresh Std :: cout <<" "Integer :" <<x<< Animal * animal = new Dog ();
) 103 Mayurish. Std:: endl; animal → → Speak D
} print (10);
In this example, more than one Employee's details with an Employee id and name can be void print (int x) print (3-14);
stored * Employee emp [30]- this an array of objects having a maximum limit of 30 { print (“Hello”);
Employees.• Two loops are used. one to take user input by calling emp[i].getdata second to std::cout << "cout<< " Integer”<< x << print (true);
print data of employee by calling the emp[i] putdata function std::endl; delete animal ;

} return 0 ;
void print (double x) }
Q5] what is operator Overloading in c++ Write a program to overload t binary operator for Q6)Implement method overloading in C++. Q7] write short note on Association, Aggregation, Composition.
concatenating two strings. →1)Method overloading is the process of overloading the method that has the same name → (a)i) Association is a relationship between two classes where one class uses the
→ (A)(1) Operator overloading is a type of Polymorphism in which an operator is overload but different parameters. The return type of methods can be the same or different functionalities provided by the other class. (ii) One object is aware of another, it Contains a
to give user defined meaning to it 2)overloaded operators are functions with special names 2)Syntax pointer or reference to another object
the keyword operator followed by the symbol for the operator being defined. int sample (a) Representation
3) overloaded operator is used to perform operation on user defined Data type4)we cannot {
use operator overloading for fundamental data }
types like int, float, char and so on int sample (int a, int b)
5)Syntax {
class name } (iii) To an association, one class instance is connected to one or more instances of another
{ float sample (float a, float b) class
Public { (b) 1)Aggregation is a relationship between two class, in which one class, known as The
return type operator symbol (arguments) } aggregate class contains a pointer or reference to an object of another class,
{ Here, the sample is the name of the method. This methods has different arguments. known as component class 2) The component class can exist independently the aggregate
} 3)Program class, and at or be shared by multiple aggregate classes. in other words, the lifetime of the
}; #include <io stream> component class is not controlled by the aggregate class
Note: (1) Return Type is the return type of the function 2) operator is a keyword. 3)symbols using namespace std; Representation
is the operator we wont to overload. Like : +, <,-, ++,etc. class addition Child class parent class
(B) Program { X y
#include <iostream> void main() public: [(c)1) Composition is a relationship between two classes in which one class, known as the
#cence include <conio.h> { int addmethed (int x, int y) composite class contains an object of another class known as corpponent class Member
using namespace std; String str1, str 2, str3; { variable. 2) The composite was the component class cannot exist independently of the
class string str 1. input (); return x + y; composite class. In other words, the lifetime of the component class is controlled by the
{ str2, input () } composite class.
char str[20]; str3 = str1+str2; int add method (int x, int y, int z) Representation
public: str3. display (); return x+ y + z ; example-
void input () getch () ; } class circle
{ }o/p }; {
cout <"Enter your string: "; Enter your String : Ram int main (void) Private :
cin-getline (str, 20); Enter your string: Kumar { Point center ;
} String: Ramkumar addition add ; };
void display ( ) Cout <<add .addmethod (2,3) <endl;
{ cout <<add.addmethod (2, 3, 6) <<endl ; Circle Point
Cout<<”String:"<<str ; Here Concatenation occurred. strings. of return 0;
} two }
string operator + (string s) output:
{ 5
String obj ; 11
strcat (str, s. str); 29
strcpy (obj. Str, str); Here we have written a program to implement mothed overlooking with a different number
return obj ; of arguments. Here add method is the name of the methods used for overloading the
} method. Here we have used some return type for both methods. In the first method. two
}; arguments are passed. This method will add two number & print the result. In the second
method three arguments are
passed. This will and three integer numbers. and will point the result.
Q1/ Explain advantages provided by inheritance and types of inheritance [Q5] Implement Array of pointer to object. [65] what is polymorphism? Explain type of it.
→The process of getting property of one class into another class is called inheritance → #include <iostream> my Array [0]-> myfunction(); → 1)It means one name many form, so we can say that in this type of programming same
1) Code reusability. class Myclass for (int i=0;}<10; 1++) function is used to perform different kind of opération. 2) It is an important part of object
When child class inherits the properties and functionality of parent class, we need not to { { oriented programming
write. same code again in child class public: delete myArray[i]; 3)Types Of Polymorphism (i) Compile Time Polymorphism (ii) Runtime Polymorphism.
(2) Readability void my Function () } (i) Compile Time Polymorphism
This makes it easier to reuse the code, makes us write less code and the Code becomes { return 0; This type of polymorphism is achieved by function overloading or operator overloading
more readable (3) Helps to reduce code redundancy std::cout << Hello, world!"<< std::endl; } (a) Function overloading.
Types of Inheritance"- } This code creates an array of 10 pointers to The function with same name with different parameter is called Function carloading
(a) Single Inheritance }; My Class objects, creates 10 Myclass' (b) Operator overloading
The Inheritance in which a derived class is inherited from the only one base class. int main() objects using the 'new! Operator, assign C++ has the ability to provide the operators with a special meaning for a data type, this
(b)Multiple Inheritance { their addresses to the elements of the ability is known as Operator overloading
when two or more than two classes are inherited by a single class simultaneously called Myclass* my Array [10]; 'myArray' array calls the MyFunction () (ii) Runtime. Polymorphism
multiple Inheritance. Here atleast three classes are compulsory. for (int i = 0; i < 10; i++) member function of the first object in the This type of polymorphism is achieved by function overriding.
{ array, and finally deletes each object Function with same name and same parameter is called function "Overriding. As it is not
(c)Multi level Inheritance myArray [1] = new Myclass; created by the 'new' operator to free the possible to make two function with Same name & Same parameter in a single class
→ when first class is inherited by second class, second class is inherited by third class and so } memory. therefore to implement function overriding derived class is used.
on called by multilevel Inheritance. Here this type of inheritance each derived class is the
base class for the next class. Q6) Explain the concept and write the program for the following mothed overriding Virtual
(d) Hierarchical Inheritance Q4] How to create a friend function for two classes explain with example? function. Rire virtual function /abstract class this pointer
→when a single class is inherited by two or more than two classes Simultaneously called → If a function is defined as a friend function then, the private data protected data of a →(i) Method overriding *Method overriding is a technique used to inheritance to allow a
hierarchical Inheritance. Derived class may be two but Base class should be one. class can be accessed using the function, with the help of the keyword friend. derived class to provide a specific implementation of a method that is already defined in its
(e) Hybrid inheritance. #include <iostream> public: base class. *To override a method in a derived class the method must have the same name,
→ The combination of two or more than two inheritance is called Hybrid Inheritance. It can using namespace std; class B() return type, and parameter as the method in the base class. The function in the derived
be combination of any two or more than two inheritance. class class B ; { class must also be declared with the virtual keyword, which indicates that the function Can
class class A b=0 ; be override
Q2)Implement hierarchical inheritance with constructor in base and derived class. { ) example
→ private: void set B(int y) class Base { std::cout << "Hello from Derived !”<<
#include <io streams > Class Dog & public Animal int a; { public: std::endl;
using namespace std; { public: b=y ; ritual void sayHello (){ }
class Animal protected: Class A () } std::cout <<" Hello from Base ! "<< };
{ string breed: { friend void add (Class A obj A, class B obj B ) std::endl’ int main(){
protected: public : a = 0; { } Base *base = new Derived ();
String names ; Dog (strings n, sting b): Animal (n) } cout << "Sum of a and b is: "<< }; base → SayHello();
public: { void set A(int x) obj A.a + ObjB. b <<endl; class Derived : public Base { delete babe;
Animal (string n ) Breed = b; { } public: return 0;
{ Cout<< "Dog constructor Called "« endl; A= x; int main(){ void sayHello () override { }
name =n; } } class A obj A;
cout << "Animal constructor called "<<endl; Void back () fiind void add (Class obj A, class B obj B) ; class B obj B ;
} { // friend fn declaration. obj A.setA (5);
Void eat () cout <<"woof woof!" <<endl; }; obj B.set B(10);
{ } class class B add(objA, obj B);
cout <" The animal is eating”<<endl ; }; { return 0;
} private: }
}; int b ;

(ii) Virtual function (iv) This pointer How to access values using pointers?
It is a member function is a member function which is declared within base class and is "This is a keyword that refers to the current instance of the class. There can be 3 main usage → Declare a pointer variable of the same data type as the variable where values you wants
redefined by derived class virtual punctions ensure that the correct function is called for an of this keyword in c++ to access.2) Assign the address of the variable where value you want to access to the
object, regardless of the type of reference used for function call • It can be used to pass current object as a parameter to another method • It can be used to pointer variable. You can use the address of operator & to get the address of variable
example refer current Class instance variable. * it can be used to declare variable example
#include <iostream> public: Example #include < io stream>
using namespace std; void area() #include <iostream> cout <<id <<" "! <<name <<" "<< using namespace std;
class Base int ar h=20w= 30; using namespace std; salary <<endl; int main()
{ ar =h*w; class Employee } {
public: cout <<"Derived class: Area =" <<ar; { }; int num= 10;
virtual void area () } Public: int main (void) int*ptr= &num;
{ }; int id; { cout<<" value of num:<<* ptr = 20)<< endl;
Int ar,h=20, w=30; int main() string name ; Employee e1=Employee (101, "Sonu", * ptr=20;
Ar=h*w; { float salary ; 890000)" cout << " New value of um." <<num <<endl;
cout <<" Base class: Area = " <<ar; Base *b ; Employee (int id, string name float salary) Employee e2=Employee (102, return 0;
} Derived d ; { "Nakul",59000) }
}; B=&d; this →id =id; e1.display () ; O/P
class Derived :public Base B →area () ; this →name=name; e2.display () ; value of num:10
{ } this→salary=salary; return 0; Now value of num:20
} } we declared an integer variable num and intializedit to 10. We then declared a pointer
iii) Pure Virtual Function void display () O/P Variable ptr of type int * and assigned the address of num' to it uing the address of operate
A pure virtual function is a function declared in the bass class that has no definition relative { 101 Sonu 890000 The access the value of 'num' using ptr, we used the dereference operator ‘*’ and printed
to the base class A class containing the pure virtual function cannot used to declare the 102 Nakul 59000 the value to the console. We then changed the value of num using ptr by asigning a new
object of its own, such classes are known as abstract base class. value to the memory location pointed to by ptr. we printed the "new value of num to the
#include <iostream> Int ar, h=20,w=30; console to verify that it was updated correctly
using namespace std; ar=h*w;
class Base cout<<"Derived class :Area = " <<ar; Q.9]what are the different file Operation and file modes? File mode
{ } → file modes
public: }; (i)ios :: in --Searches for the file and opens it in the read mode only (if the file is found.
Virtual void area ()=0; Int main () (ii) fos:: out ---Searches for the file and opens it in the write mode. If the file is found, its
}; { content is over written.
Class Derived: public base Base *b; (ii)ios ::opp --Searches for the file and opens it in the append mode.
{ Derived d; (iV) ios:: binary --- Searches for the file and opens the file in a binary mode to perform binary
public: b=&d; input/output file operations.
void area() b→area () ; (v) To: trun c --Searches for the file and opens it to truncate or deletes all of its content.
{ } (vi)ios::noncreate" ---Searches for the file and if the file is not found, a new file wil not be
created
File operations
1)open function ()
Syntax!
file stream object ("filename", mode); The open () opens the file in a specific mode to let
you perform a specific file operator
2)read ()
reading data from file..
3)write ()
Writing data in a file
4) close () --* closing a file
Q10] How read and write operation Any form in C++?. Q.11] Design a class Diagram for Hospital management System Q1] Explain N-Queen problem
→1) writing to a file →The N-Queens problem is a classic problem in computer science at that involves placing N
#include <iostream> → chess queens on an N X N then board such that no two queers attack each other. In other
#include < f stream > words, no two queens can be placed in the same row, column, or diagonal.
using name space std;
The problem can be solved using a backtracking algorithm, where the algorithm tries to
int main( )
place a queen in each row of the board, and backtracks if it finds that the placement riolates
{
F stream new-file the constraint of no two queers attacking each other.
new_file.open("new_file_ write.txt “,ios::out) The basic steps of the backtracking algorithm for the N-queens problem are as follows:
if ( ! new_file )
{ 1). Initialize an empty NXN chessboard. 2). Start with the first row and try to place
cout <<" File creation failed";
a to queen in each column of the rows checking if the placement is valid by ensuring that no
}
Else other queens can attack the current queen 3). If a valid placement is found, move to the
{ next row and repeat step 2 4). If all rows have been filled with queens, the algorithm has
Cont << "New file created"; found a valid solution to the N-Queen's problem 5- If a placement is found to be invalid,
New_file << "Learningfile handling "; backtrack to the previous row and try the next column in that row.
New_file, close();
The backtracking algorithm continues this processes until all possible have been tried, or
}
until a valid solution is found,
return 0;
} The N-Queens problem is a challenging problem due to large number of possible solutions.
O/P and the constraints on the placement of queen. It is often used as a benchmark problem for
new file_with_Notepad -DX testing the performance of backtracking algorithms and other search, algorithms.
File Edit Format View Help.
Learning File handling

Here we first create a new file, "new__file__write" using open () function since we wanted
to send output to the file so we use ios:: out. As given in the program, information typed
inside the quotes after Insertion Pointer << "got passed to the output file.
2) Reading from a file.
#include <io stream> if (! new file)
#include <io stream> cout << "No such file "; } else charch ;
Using namespace std ; while
Int main () (! new_file_cof()) { new file >>ch;
{ Cout << ch ;
I }
F new file close();
f stream new_file; return 0;
new _file.open ("new file_write_txt", ios:: in); }

Here, we read the file that generated id previous example


i.e new _file. write.
To read a file we need to use in' mede with syntax ios :: in

Explain advantage and disadvantages of Array


1)ALL the elements are 1)Array are static is nature, so it can create
stored strategically based on index no problem of size during development
2) we can easily traverse in Me 2)By the size requirement is loss than the
Array memory. wastage is more
3)we can fix the clement based on their (3) Array is homogenous in nature but to
index no- at any time real life we require to store elements of
4)we can also search for particular element different type.
from array by traversing
5) we can sort array using sorting
techniques & can delete elements from
array.

Q)Different between Recursion and Iteration



Recursion Iteration
1)it terminates when a base case is reached 1)it terminates when a condition is proren
2)Recursive function calls itself to be false
3)in terms of execution Recursion is slower 2)iterative function implemented using
process loops
4)Each recursive call requires extra space 3)it is faster process
on the stack frame (memory) 4)Each iteration don not require extra.
5) if we get infinite recursion, the program space
may run out of memory and result in stack 5) An infinite loop could. loop forever since
overflow there is no extra memory being created
6) solutions to some problem are easer to 6)Iterative solutions to a problems may not
formulate recursively always. be as obvious as a recursive
solution

You might also like