i am new in this and i am working on App of media player and recording app. in which i have shown song list of device in the listview and recording start / stop / play. Now i want to convert that .mp3 recorded file into .mp4 and one image will show on behalf of a video in that file. Help me to achive this i have no idea and i refer many links and i didnt find anything.
-
2Re: the second sample, looking just at the code, what is the value of `temp` at the moment you assign `b=temp;` If you're answer is, "I don't know", you're in good company because neither does your program. It invokes *undefined behavior* – WhozCraig Jul 12 '18 at 06:35
-
it will returning the value of temp variable because your assigning b=temp and then printing it the second logic is wrong. – Ankita Mehta Jul 12 '18 at 06:38
-
TBH `int a=10` looks fine, even in the class. What's the smallest program that causes the error, what's the exact errror? Instead of asking 3 bad questions, each of which is missing a lot, please ask 1 good question. – MSalters Jul 12 '18 at 06:39
-
It might help of you focus your post on one single question, with an accompanying [mcve]. – juanchopanza Jul 12 '18 at 06:40
-
You name a member function after your class??? If you want to write a constructor: these have no return values, i. e. you need to drop `void`. You might prefer the initialiser list against assignment: `swap() : a(10), b(20) {}` – Aconcagua Jul 12 '18 at 06:42
-
*"\\cannot declare here"* - if you mean compiler complains and you cannot use *default initialisation*, maybe you are using an outdated compiler? Maybe you need to add appropriate compiler flags (e. g. GCC: one of `-std=c++11/14/17/0x/1y`, depending on version...). – Aconcagua Jul 12 '18 at 07:02
-
can anybody help with this question? – Drashti Jan 18 '19 at 05:09
-
@Drashti Add another question, you have edited the same question that you ask previously – HarshGiri Jan 21 '19 at 18:21
3 Answers
Please check this link for your first question:
Why can't we initialize class members at their declaration?
Usually constructor is use to initialize value to data variables of class.
For 2nd Question: If data member is not initialize after creation of object, It will contain garbage value. So initialize or assign suitable value to as per your need.
Check below code:
#include<iostream>
using namespace std;
class swap_values
{
int a, b, temp;
public:
swap_values(){
a=0;b=0;temp=0;
}
swap_values(int x, int y){
a = x;
b = y;
temp = 0;
}
void swapped()
{
temp = b;
b=a;
a=temp;
}
void print(){
cout<<"a: "<<a<<" b: "<<b<<endl;
}
};
int main()
{
int x =10; int y = 20;
swap_values obj(x, y);
obj.print();
obj.swapped();
obj.print();
return 0;
}

- 408
- 2
- 12
Everything can be done in better ways but just using your code this will work for you -
#include <iostream>
using namespace std;
class Swap {
private:
int a,b,temp;
public:
Swap()
{
a=10;
b=20;
temp=0;
}
void swapNums()
{
temp=a; a=b; b=temp;
cout<<a<<" " <<b<<endl;
}
};
int main() {
Swap s;
s.swapNums();
return 0;
}
You can avoid using class name as some function name. You can instead use constructor without a return type where you can initialise the member variables. swap method looks fine.
i am not able to initialize my variable in class.
class swap { int a=10; \\cannot declare here int b=20; \\ cannot declare here }
Since C++11, this is fine, you can have default member initialization.
The error is due to missing semicolon after }
.
why it has garbage value with b ??
a=b; b=temp; temp=a;
Since temp
was never initialized before assigning it to b
, temp
has an indeterminate value.
Any usage will lead to undefined behavior.
Here's a simple Swap struct
:
struct Swap
{
int a = 10; // default member initialization
int b = 20; // default member initialization
Swap(int a = 20, int b = 10): a(b), b(a) {}; // swap on initialization
// using member initializer list
};
Swap s;
std::cout << s.a // 20
<< s.b // 10
<< std::endl;
In this example, default member initialization is "obsolete" / "redundant" due to member initializer list.

- 11,804
- 3
- 34
- 67
-
Suppose you should mention that now, with complete constructor, default initialisation just got redundant/obsolete... – Aconcagua Jul 12 '18 at 06:58
-