Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
using namespace std;
int main()
{
int k,a=3,b=4;
test*p=new test(3,2);
p->show();
p->get(a,b);
cout<<a<<" "<<b<<endl;
p->set(4);
delete p;
p=new test [3];
p[1].get(a,b);
p[1].show();
cout<<a<<" "<<b<<endl;
b-=7;
p[2].get(a,b);
p[2].show();
cout<<a<<" "<<b<<endl;
p[0].show();
delete []p;
system ("pause");
}

class test{
int x,y;
public:
test(int m=2,int n=3)
{
x=m;
y=n;
}
void set (int t)
{
x=t;
y=t+t;
}
void get (int & m, int & n)
{
x=m;
y=n*2;
m=x+1;
n=y-2;
}
void show ()
{
cout<<x<<" "<<y<<endl;
}
~test()
{
cout<<x*y<<endl;
}

delete []p;
system ("pause");
}
class test{
int x,y;
public:
test(int m=2,int n=3)
{
x=m;
y=n;
}

void set (int t)
{
x=t;
y=t+t;
}
void get (int & m, int & n)
{
x=m;
y=n*2;
m=x+1;
n=y-2;
}
void show ()
{
cout<<x<<" "<<y<<endl;
}

~test()
{
cout<<x*y<<endl;
}
};

What I have tried:

I have tried in code block and in microsoft visual both compilers giving error ?
i upload image of errors on the link down
https://ibb.co/ieRDMb
Posted
Updated 30-May-19 0:43am
v4
Comments
CodeWraith 2-Jan-18 21:04pm    
Then it may be a good idea not to keep us guessing. What error did the compilers show? And usually both CodeBlocks and VS tell you at what code ine they have a problem.

I would suggest to look at the error messages, look up the meaning of the error and then taking a good look at the code line. If that does not help, come back and post the error message and don't forget to mention which code line here.
Member 13604021 2-Jan-18 22:11pm    
the list of errors are here,i could not upload image so this is the link of the image ttps://ibb.co/ieRDMb, thank you .
CodeWraith 2-Jan-18 23:13pm    
These errors do not correspond to the updated code you have posted, at least as far as I can see. So we are back at the beginning. Now, we can play this endlessly or you can try to read the error messages and try to resolve them one by one. What, for example, is so hard to understand about something like 'test' was not declared in this scope? Do you really need us to tell you that you must declare variables (with the correct type!) before you use them?

These are all very basic mistakes. I know that this can be frustrating at the beginning, but you will not get very far if you don't learn to read the error messages. look at the code and fix it.

Start with the topmost error. Read the error message, look it up or ask us if you still don't understand what it means. And then you fix it. Recompile and repeat this procedure until you have no more errors or warnings.
Member 13604021 3-Jan-18 0:06am    
The thing that i don't understand that "test" is not a variable it is a class name and it should not be declared in the main as far as i know,
when we are working with classes it is enough to write class name and then create an object to this class and "*p" is an object name in which dynamic array "new test" is assigned to the pointer if i not misunderstood the code, and i don't really understand where is mistake exactly because code is from book and i need to trace it , thank you for replying !
CodeWraith 3-Jan-18 1:36am    
'test' is a class name, that's right. Declaring a class always introduces a new self defined complex datatype. To get an instance of the class (anywhere you need it, not just in main(), you must first declare a pointe to your class type and assign it a value by using the 'new' operator. Let's put these two things into two separate lines to make it more clear:

test* p; // this is just a normal variable declaration
// a pointer to an object of the type 'test'

p = new test(1, 2); // here we actualy give p a value.
// memory is allocated for the new object
// and the parameters 1 and 2 are passed to the
// constructor

Now you can use p to access the new object and call methods and do whatever you wanted to do with it. But there is one more important thing you should do when you don't need it anymore:

delete p; // this frees the memory that was used by the object

If you don't clean up your program will have massive memory leaks. In the main() function this may not be a problem, but it would still be sloppy. In othe functions or methods this is othing less than a bug.

1 solution

Just looking at your first few lines:

int *p;          // here you declare p as 
                 //a pointer to an int
p=new test (3,2) // and here you assign an 
                 //instance of test to it
                 // Also: You forgot something
                 // at the end of te line! (;)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900