C++ programming assignment, answer the following questions 1. what are the point
ID: 3845301 • Letter: C
Question
C++ programming assignment, answer the following questions
1. what are the pointer basics such assigning a pointer variable to an object and using the pointer to manipulate the object?
2. what are the main operations of the string class and how to use a string object as if it were an array of characters?
3. How to use strings in functions (as arguments and return values)?
4. How to create and use arrays of objects?
5. What a vector object is used for and the main operations of the vector class. Be comfortable with vectors of objects, passing a vector as an argument to a function (by value or by reference), returning a vector from a function?
Explanation / Answer
Solution:
1)
So pointer is a variable which contains the address of another variable. Pointers are very helpful in so many cases.
Like if you are using array data structure, pointers help
The main idea behind using a pointer (i.e assigning a pointer variable to an object and using the pointer to manipulate the object is) to access the variable throughout the program or to support data structures like Stack, Queue, Array and linked list. Where with the help of pointers we can pass the values across functions by using call by reference method.
int *a (a is a pointer type variable who will hold an address)
and to access the value present at in variable a we use de referencing that is *a
2)
A string is defined as string str[]= "This is a string";
The above string contains 17 characters (one character is NULL character, which will define that the string is over).
Other ways of defining an string
str [17]= {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ''};
or
str []= {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ''};
Example:
3)
The example is given below to use string in function.
#include<stdio.h>
#include<conio.h>
int len(char*);
int main()
{
int i;
char str[10];
printf(" Enter the string");
scanf("%s",str);
i=len(str);
printf("%d",i);
}
int len(char *str)
{
int i;
for(i=0;str[i]!=0;i++);
return i;
}
4)
The syntax of array of objects goes like this
class_name array_name [size] ;
Example:
#include<iostream>
using namespace std;
class cars
{
char name [30];
float price ;
public:
void getdata ();
void putdata ();
} ;
void cars :: getdata ()
{
cout<<" Name: ”;
Cin>>name;
cout<<" Price:”;
cin>>price;
}
void cars :: putdata ()
{
cout<<" Name:"<<name<< " ";
cout<<"Price:"<<price<< " ”;
const int size=5 ;
int main ()
{
cars car[size] ;
for(int j=0; j<size; j++)
{
cout<<"Please input details about the car "<<(j+1)<<" ";
car[j].getdata();
}
for(int j=0;j<size; j++)
{
cout<<" Car "<<(j+l)<<" ";
car[j].putdata() ;
}
return 0;
}
I hope this helps, please hit the thumbs up if you liked it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.