9. سوال:چیست؟ شده داده ورودی ازای به زیر کد خروجی
ورودی:
4
3512
کد:
int main() {
int n;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int* b = a + n;
while (b-- != a)
cout << *b << "t";
}
15. int main()
{
Circle foo(10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz{ 30.0 }; // uniform init.
Circle qux = { 40.0 }; // POD-like
cout << "foo's circumference: " << foo.circum() <<
'n'; //prints: foo's circumference: 62.8319
return 0;
}
16. مثال:
#include <bits/stdc++.h> //or #include <iostream> if in VS
using namespace std;
class Query
{
public:
int L, R;
};
void printQuerySums(int a[], int n, Query q[], int m)
{
for (int i = 0; i < m; i++) {
int L = q[i].L, R = q[i].R, sum = 0;
for (int j = L; j <= R; j++)
sum += a[j];
cout << "Sum of [" << L << ", " << R << "] is " << sum << endl;
}
}
22. #include <iostream>
using namespace std;
class Box
{
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) //
Constructor definition
{
cout << "Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
23. double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume(); // returns 1 if the
statement is true, otherwise 0 would be returned
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
24. int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if (Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1" << endl;
}
25. else
{
cout << "Box2 is equal to or larger than Box1" << endl;
}
return 0;
}
خروجیباشد می زیر شکل به کد این:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
تذکر:از استفادهthisمیشود محسوب خطا ایستا توابع در.
32. // pointer to classes example
#include <iostream>
using namespace std;
class Rectangle
{
int width, height;
public:
Rectangle(int x, int y) : width(x), height(y) {}
int area(void) { return width * height; }
};
39. class Daughter : public Mother
{
public:
Daughter(int a)
{
cout << "Daughter: int parameternn";
}
};
class Son : public Mother {
public:
Son(int a) : Mother(a)
{
cout << "Son: int parameternn";
}
};
40. int main()
{
Daughter kelly(0);
Son bud(0);
return 0;
}
میباشد زیر شکل به فوق کد خروجی:
Mother: no parameters
Daughter: int parameter
Mother: int parameter
Son: int parameter
42. // pointer is destroyed because it goes out of scope,
// but not the object it pointed to. memory leak
if (1) { Foo *myfoo = new Foo("foo"); }
// pointer is destroyed because it goes out of scope,
// object it points to is deleted. no memory leak
if (1) {
Foo *myfoo = new Foo("foo");
delete myfoo;
}
// no memory leak, object goes out of scope
if (1) { Foo myfoo("foo"); }
54. class Rectangle : public Shape
{
public:
virtual void draw()
{
cout << "Drawing rectangle at " << posX << " " << posY << endl;
}
};
class Hexagon : public Shape
{
public:
virtual void draw()
{
cout << "Drawing hexagon at " << posX << " " << posY << endl;
}
};
55. int main()
{
const int NUM_SHAPES = 3;
Shape* shapeArray[] = { new Hexagon(), new Rectangle(), new Hexagon() };
int posX = 5, posY = 15;
for (int k = 0; k < NUM_SHAPES; k++) //set positions of all the shapes
{
shapeArray[k]->setPosition(posX, posY);
posX += 10;
posY += 10;
};
for (int j = 0; j < NUM_SHAPES; j++) //draw all the shapes at their positions
shapeArray[j]->draw();
return 0;
}
67. // friend class
#include <iostream>
using namespace std;
class Square;
class Rectangle
{
int width, height;
public:
int area() { return (width * height); }
void convert(Square a);
};
68. class Square
{
friend class Rectangle;
private:
int side;
public:
Square(int a) : side(a) {}
};
void Rectangle::convert(Square a)
{
width = a.side;
height = a.side;
}
82. #include <iostream>
using namespace std;
void error(char* s) { cout << s << endl; }
int main() {
void(*f1)(char*) = &error; //ok
void(*f2)(char*) = error; // also ok; same meaning as &error
f1("f1 function without dereference."); //ok
(*f1)("f1 function with dereference."); //also ok
f2("f2 function without dereference.");
(*f2)("f2 function with dereference."); //also ok
}
84. // writing on a text file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile("example.txt"); // if no file exists, create one
// ofstream myfile; myfile.open("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.n";
myfile << "This is another line.n";
myfile.close(); // close the stream
}
else cout << "Unable to open file";
return 0;
}
85. // reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream myfile("example.txt");
if (myfile.is_open())
{
// getline returns 0 when there is nothing to read
while (getline(myfile, line)) // reading a line from myfile stream
cout << line << 'n';
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
89. دستور با کردن کار راهنمایseek:
// position to the nth byte of fileObject (assumes ios::beg)
fileObject.seekg(n);
// position n bytes forward in fileObject
fileObject.seekg(n, ios::cur);
// position n bytes back from end of fileObject
fileObject.seekg(n, ios::end);
// position at end of fileObject
fileObject.seekg(0, ios::end);
90. // obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
streampos begin, end; // data type = File-Position Pointer
ifstream myfile("example.bin", ios::binary);
begin = myfile.tellg();
myfile.seekg(0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end - begin) << " bytes.n";
return 0;
}
93. #include <iostream>
using namespace std;
namespace first{
int x = 5;
int y = 10;
}
namespace second {
double x = 3.1416;
double y = 2.7183;
}
int main() {
using first::x;
using second::y;
cout << x << 'n';
cout << y << 'n';
cout << first::y << 'n';
cout << second::x << 'n';
return 0;
}
111. class A
{
public:
void Const_No(); // nonconst member function
void Const_Yes() const; // const member function
};
int main()
{
A obj_nonconst; // nonconst object
obj_nonconst.Const_No(); // works fine
obj_nonconst.Const_Yes(); // works fine
const A obj_const = A(); // const object
obj_const.Const_Yes(); // works fine (const object can call const function)
obj_const.Const_No(); // ERROR (const object cannot call nonconst function)
}