📅  最后修改于: 2023-12-03 14:58:30.602000             🧑  作者: Mango
GATE-CS-2016 (Set 1) - Chapter 32 is a part of the GATE 2016 (Computer Science and Information Technology) exam. This section focuses on programming concepts in C and C++ languages. The questions in this chapter test the candidate's understanding of basic programming concepts, data structures, algorithms, and problem-solving skills.
The syllabus of GATE-CS-2016 (Set 1) - Chapter 32 includes the following topics:
#include <stdio.h>
#include <stdlib.h>
int *fun(int *p)
{
p++;
return p;
}
int main()
{
int a[5] = {10, 20, 30, 40, 50};
int *ptr = a;
ptr = fun(ptr);
printf("%d ", *ptr++);
ptr = fun(ptr);
printf("%d ", *ptr);
return 0;
}
Output: 30 40
Explanation: The function fun
receives a pointer to an integer and increments it by one. The main
function initializes an integer array a
and a pointer ptr
to the start of a
. Then, ptr
is passed to fun
function, where it is incremented by one. The returned value of fun
is stored in ptr
. The value of ptr
is now pointing to the element at index 1 in a
, which has the value 20.
After that, ptr
is again passed to fun
function, where it is again incremented by one. Now, ptr
is pointing to the element at index 2 in a
, which has the value 30. The first printf
statement prints the value pointed by ptr
(which is 30), and the second printf
statement prints the value pointed by ptr
(which is 40).
#include <iostream>
using namespace std;
class A
{
public:
int a;
A() { a = 10; }
void display() { cout<<a<<endl; }
};
class B: public A
{
public:
int b;
B() { b = 20; }
void display() { cout<<a<<" "<<b<<endl; }
};
int main()
{
A *ptr = new B();
ptr->display();
delete ptr;
return 0;
}
Output: 10
Explanation: This program creates two classes, A
and B
. B
is derived from A
using public inheritance. A
has a public integer member variable a
, and a constructor that initializes a
to 10. B
has a public integer member variable b
, and a constructor that initializes b
to 20.
The main
function creates a pointer ptr
of class A
type and assigns it the address of a dynamically created object of class B
type. The display
function of class B
is overridden to print both a
and b
variables of the object. However, the display
function of class A
is not overridden.
When a member function is called from a pointer to an object of a derived class using a pointer of its base class type, then the function of the base class is called if it is not overridden. In this case, ptr
is of A
type, so ptr->display()
calls the display
function of class A
, which only prints the value of a
(10).